jQuery in Chrome Console (8.0.552.237)

前端 未结 2 2074
野性不改
野性不改 2020-12-31 03:32

It appears that jQuery selectors are not functioning in the Chrome Console. Any type of selector returns \"null\". The scripts do properly run the javascript, however.

相关标签:
2条回答
  • 2020-12-31 04:22

    Just run the following command on the console to make it work:

    $ = jQuery
    
    0 讨论(0)
  • 2020-12-31 04:27

    I uncovered the cause of this in my own question.

    The console injects its own function (just a shorthand) for document.getElementById(), aliased to $, which shadows jQuery's $. Easy way to check this: when you're at a breakpoint, and jQuery seems to be broken, compare the following in the console:

    • jQuery
    • $
    • window.$

    The first and last will be jQuery proper, the local $ is something like:

    function () {
        return document.getElementById.apply(document, arguments)
    }
    

    This is because code run from the console is wrapped in a with statement:

    with (window ? window.console._commandLineApi : {}) {
    with (window) {
        // the actual code you typed in here
    }   
    }
    

    and window._commandLineApi.$ is the function that shadows jQuery.


    Found the bug in Chromium for this: http://code.google.com/p/chromium/issues/detail?id=70969

    0 讨论(0)
提交回复
热议问题