में कॉलिंग ऑब्जेक्ट और फ़ंक्शन के बीच का अंतर मैं दो फाइलों में लिख रहा हूं - एक HTML है और एक जावास्क्रिप्ट है। तो एक वस्तु कॉल करने के लिए मैंजावास्क्रिप्ट
document.getElementById("nameObj").onmouseover = changeMe;
करते हैं और JavaScript फ़ाइल में मैं
changeMe = function()
{
//and here i write the function
}
है, लेकिन अब मैं अपने कोड का अनुकूलन करने और उस में वस्तुओं के साथ एक समारोह कॉल करने के लिए कोशिश कर रहा हूँ। मैंने अनुभाग (उनमें से 4) बनाए और मैं onmouseover
और onmouseout
के साथ रंग बदलने की कोशिश कर रहा हूं। HTML का कोड है:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"> </script>
<title> test 2</title>
</head>
<body>
<header> </header>
<div id="wrapper">
<main>
<section class="mysection" id="section1"> </section>
<section class="mysection" id="section2"> </section>
<section class="mysection" id="section3"> </section>
<section class="mysection" id="section4"> </section>
</main>
<div class="clear"> </div>
</div>
<footer> </footer>
<script>
(function(){
var sec = document.getElementsByClassName("mysection");
for(var i=0; i<3; i++)
{
sec[i].onmouseover=changeMe(sec[i], i);
sec[i].onmouseout=changeBack(sec[i]);
}
})();
</script>
</body>
</html>
और यहाँ जेएस है:
function changeMe(t_section, count)
{
if(count==0)
{
t_section.style.background="yellow";
}
if(count==1)
{
t_section.style.background="blue";
}
if(count==2)
{
t_section.style.background="green";
}
if(count==3)
{
t_section.style.background="red";
}
};
function changeBack(t_section)
{
t_section.style.background="gray";
};
लेकिन यह काम नहीं कर रहा। मैंने गलत क्या किया?
की संभावित डुप्लिकेट [जावास्क्रिप्ट बंद अंदर लूप - सरल व्यावहारिक उदाहरण] (http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple- व्यावहारिक उदाहरण) – MinusFour