简体   繁体   English

添加document.ready时代码不起作用

[英]Code not working when document.ready is added

the code below works fine on jsFiddle without $(document).ready(function() { and the closing brackets. (ex: http://jsfiddle.net/cEDYD/ ) but soon as I put the code on my webpage with the document ready it stops works. Am I missing something? I've included jquery 下面的代码在没有$(document).ready(function() {和右括号。(例如: http//jsfiddle.net/cEDYD/ ))的jsFiddle上工作正常,但是当我将代码与文档准备就绪,它可以停止工作。我是否缺少某些东西?

<script type="text/javascript">
$(document).ready(function() {
    function showDiv(element, pro2) {
        if (pro2.children("option:selected").val() == "cpl") element.show();
        else element.hide();
    }

    var myElement = $("div#pro2");
    var mypro2 = $("select#ptype");

    $("select").change(function() {
        showDiv(myElement, mypro2)
    });
});
</script>

Is there a reason you wouldn't want this wrapped in a document ready handler? 您是否有理由不希望将其包装在文档就绪处理程序中?

Appears the DOM is not ready at the time your javascript is executing. 在执行JavaScript时,似乎DOM尚未准备就绪。

Check the javascript console. 检查JavaScript控制台。 Google Chrome is excellent for JS debugging. Google Chrome非常适合JS调试。

problem ended up being a mistype character in my css file. 问题最终是我的css文件中的错误键入字符。 -thanks for the testing guys -感谢测试人员

Your jsfiddle has a setting that automatically wraps the code in document.ready() . 您的jsfiddle具有一个设置,该设置可自动将代码包装在document.ready() That's why it works without the ready() in the jsFiddle (see where is says onload in the upper left panel). 这就是为什么它在jsFiddle中没有ready()情况下可以工作的原因(请参阅左上方面板中的onload )。

document.ready() is needed in a regular web page unless you place the javascript after the body HTML. 常规网页中需要document.ready() ,除非将javascript放在正文HTML后面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM