函数不被调用 [英] Function is not Called
本文介绍了函数不被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
function setClasses(){
document.getElementsByClassName(gchoice_35_0)[0] .onclick = sedanShow;
document.getElementsByClassName(gchoice_22_0)[0] .onclick = sedanShow;
document.getElementsByClassName(gchoice_34_0)[0] .onclick = sedanShow;
document.getElementsByClassName(gchoice_34_1)[0] .onclick = suvShow;
document.getElementsByClassName(gchoice_35_1)[0] .onclick = suvShow;
document.getElementsByClassName(gchoice_22_1)[0] .onclick = suvShow;
document.getElementsByClassName(gchoice_22_2)[0] .onclick = vanShow;
document.getElementsByClassName(gchoice_35_2)[0] .onclick = vanShow;
document.getElementsByClassName(gchoice_34_2)[0] .onclick = vanShow;
}
window.onload = setClasses;
setClasses()函数似乎不运行。但是,当我通过FireBug的控制台手动调用它时,它确实有效。
代码放在我网页的标题中。
任何帮助表示赞赏。
完整的html代码片段:
< HEAD>
......
< script type =text / javascript>
函数setClasses(){
document.getElementsByClassName(gchoice_35_0)[0] .onclick = sedanShow;
document.getElementsByClassName(gchoice_22_0)[0] .onclick = sedanShow;
document.getElementsByClassName(gchoice_34_0)[0] .onclick = sedanShow;
document.getElementsByClassName(gchoice_34_1)[0] .onclick = suvShow;
document.getElementsByClassName(gchoice_35_1)[0] .onclick = suvShow;
document.getElementsByClassName(gchoice_22_1)[0] .onclick = suvShow;
document.getElementsByClassName(gchoice_22_2)[0] .onclick = vanShow;
document.getElementsByClassName(gchoice_35_2)[0] .onclick = vanShow;
document.getElementsByClassName(gchoice_34_2)[0] .onclick = vanShow;
}
函数sedanShow(){
document.getElementById(sedan)。style.display =inline
document.getElementById(suv) .style.display =none
document.getElementById(van)。style.display =none
}
函数suvShow(){
document.getElementById sedan)。style.display =none
document.getElementById(suv)。style.display =inline
document.getElementById(van)。style.display =无
}
函数vanShow(){
document.getElementById(sedan)。style.display =none
document.getElementById(suv)。style .display =none
document.getElementById(van)。style.display =inline
}
window.onload = setClasses;
< / script>
......
解决方案
您可以随时使用JQuery
< script type =text / javascriptsrc =https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js>< / script>
< script type =text / javascript>
$(document).ready(function(){
setClasses();
});
< / script>
编辑
示例JQuery Event Refactor:
$(。gchoice_35_0)。click(function(){
// Handler Code ...
});
I have some javascript that is supposed to run after the window loads but for some reason, it never runs.
Here's my code:
function setClasses(){
document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
}
window.onload = setClasses;
The setClasses() function doesn't seem to run. It does however work when I manually call it through the console of FireBug.
The code is placed in the header of my web page.
Any help is appreciated.
Full html snippet:
<head>
......
<script type="text/javascript">
function setClasses(){
document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;
document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;
document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
}
function sedanShow(){
document.getElementById("sedan").style.display="inline"
document.getElementById("suv").style.display="none"
document.getElementById("van").style.display="none"
}
function suvShow(){
document.getElementById("sedan").style.display="none"
document.getElementById("suv").style.display="inline"
document.getElementById("van").style.display="none"
}
function vanShow(){
document.getElementById("sedan").style.display="none"
document.getElementById("suv").style.display="none"
document.getElementById("van").style.display="inline"
}
window.onload = setClasses;
</script>
......
解决方案
You could always use JQuery
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setClasses();
});
</script>
Edit
Example JQuery Event Refactor:
$(".gchoice_35_0").click(function(){
//Handler Code...
});
这篇关于函数不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文