如何在 Chrome 中 console.log 一个 jQuery DOM 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13974740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:09:17  来源:igfitidea点击:

How do I console.log a jQuery DOM Element in Chrome?

jquerygoogle-chromelogging

提问by Naftali aka Neal

I used to be able to do console.log(somejQueryObj);and it logged in an array all of the DOM elements that are in the object that I could click and go to the inspector.

我曾经能够这样做,console.log(somejQueryObj);并且它将对象中的所有 DOM 元素都记录在一个数组中,我可以单击并转到检查器。

Now it does something like this:

现在它做了这样的事情:

[prevObject: p.fn.p.init[1], context: , selector: ".next ()"]

[prevObject: p.fn.p.init[1], context: , selector: ".next()"]

which can confuse many people.

这会让很多人感到困惑。

How do I make it so that Chrome logs how it used to log jQuery elements?

如何让 Chrome 记录它用于记录 jQuery 元素的方式?

Here is a fiddle example

这是一个小提琴示例



I am in:

我在:

Google Chrome 23.0.1271.97 (Official Build 171054) m

谷歌浏览器 23.0.1271.97(官方版本 171054)米

回答by pimvdb

Update:I made a jQuery plugin to bring back the old style logging: jquery.chromelog.

更新:我制作了一个 jQuery 插件来恢复旧样式的日志记录:jquery.chromelog



You could create a little function to log all elements on one line:

您可以创建一个小函数来记录一行中的所有元素:

$.fn.log = function() {
  console.log.apply(console, this);
  return this;
};

Usage:

用法:

$("...").log();

回答by lamflam

To do it for each element so that you can hover over it, try something like this:

要为每个元素执行此操作以便您可以将鼠标悬停在它上面,请尝试以下操作:

$("div").each(function(){console.log(this)})?

回答by Naftali aka Neal

I have found a solutionthat would log them individually if need be (but it could clutter the log if it is BIG selector):

我找到了一个解决方案,可以在需要时单独记录它们(但如果它是 BIG 选择器,它可能会使日志混乱):

http://jsfiddle.net/maniator/ya7As/

http://jsfiddle.net/maniator/ya7As/

var log = function($selector) {
    $selector.each(function() {
        console.log(this);
    });
};
log($('selector'))?;

回答by kidwon

console.log($(...)[0]);

is another way

是另一种方式