简体   繁体   中英

jQuery selectors don't work in console

I can't for the life of me work this one out. I have js running and 'container state..' is a console log from the running js on the page. It's displaying a selector, but if i want to do anything within the console it just returns null. I'm assuming somehow i'm over writing jQuery function somewhere, as if i called jQuery

>>> $
function()

This is how i am calling a selector

Container state 3 jQuery(div.module-carousel)
>>> $('body')
null  

jQuery uses 2 namespaces, jQuery and $ . Another library could have used the $ . Try using jQuery instead of $ (assuming that it isn't overridden as well):

jQuery('body');

or wrap jQuery in an immediate function and use $ in it so you don't need to replace $ in the existing code:

(function($){
    //"$" in here is jQuery
    //code that uses $ as jQuery will work in here
}(jQuery)); //pass in jQuery and execute

你可以在控制台上编写任何命令之前使用它。

$ = jQuery.noConflict();

If your $ is overloaded (but not jQuery ), and you want to work in your console, just do the following:

$ = jQuery;

As simple as that.

For a more complete solution (real development, not just console), use @Joseph the Dreamer 's solution.

Both Firefox and Chrome define $ as a shorthand for document.getElementById in the console. This shorthand will automatically be overridden when the page defines $ .

So, load jQuery ( through a bookmarklet for example) and you can use jQuery selectors.

If you don't need jQuery-specific selectors, you can also use $$ , which is a shorthand for document.querySelectorAll , which supports CSS(3) selectors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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