jQuery in Chrome Console (8.0.552.237)

别来无恙 提交于 2019-11-29 11:26:42

问题


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.

Has anyone else noticed this change or know of a fix.

Thanks.


回答1:


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




回答2:


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

$ = jQuery


来源:https://stackoverflow.com/questions/4796103/jquery-in-chrome-console-8-0-552-237

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!