简体   繁体   English

仅在浏览器控制台上,不在页面上执行jQuery代码文档就绪的操作

[英]jQuery code document ready is not executed on the page, only from the browser console

I'm curious why the code below isn't executed when the page loaded. 我很好奇为什么页面加载后未执行以下代码。 It's in the page. 在页面中。

<script>
    if (typeof jQuery != 'undefined'){
        jQuery(document).ready(function($) {
            $("#mydiv .myclass:odd").css('color', 'green');
        });
    }
</script>

and this code is executed from the browser console after the page (the same page) loaded: 并在页面(同一页面)加载后从浏览器控制台执行以下代码:

jQuery(document).ready(function($) {
    $("#mydiv .myclass:odd").css('color', 'green');
});

Unfortunately, I don't have access to the code of that page and can't modify it. 不幸的是,我无权访问该页面的代码,并且无法对其进行修改。 However I need an answer. 但是我需要一个答案。

What could be the reason? 可能是什么原因? Thank you. 谢谢。

I guess there is unnecessary parameter. 我猜有没有必要的参数。 Should be: 应该:

$(document).ready(function() {
    $("#mydiv .myclass:odd").css('color', 'green');
});

If you have any conflicts with other libraries, please, use jQuery in no conflict mode: 如果与其他库有任何冲突,请在无冲突模式下使用jQuery:

var $j = jQuery.noConflict();

$j(document).ready(function() {
    $j("#mydiv .myclass:odd").css('color', 'green');
});
$(document).ready(function() {
    $("#mydiv .myclass:odd").css('color', 'green');
});

Please use the above code and add jquery.js in html file so that it will understand the $ parameter. 请使用上面的代码并在html文件中添加jquery.js,以便它可以理解$参数。 or add var $j = jQuery.noConflict(); 或添加var $ j = jQuery.noConflict();

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

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