javascript - 元素 $(this) 在 console.log 中输出不同的格式

标签 javascript jquery console

我循环访问具有相同类名的元素,并使用 console.log 打印该元素,如下所示:

$('.thumbImg').each(function(i) {
  console.log("i is", i, "this is", this);
});

但是 console.log 的输出是意外的,在以下格式之间随机变化:

i is 13 this is img.thumbImg
i is 14 this is <img class=​"thumbImg" src=​"images/​bookcovers/​jekyllhyde.jpg" alt>​

首选格式是第二个完整元素输出。

这是我的控制台的屏幕截图:

enter image description here

我认为这会导致稍后在程序中加载这些图像时出现问题,有人能给我指出正确的修复方向吗?

最佳答案

您可以使用:

$('.thumbImg').each(function(key,element) {
alert("i is "+ key+ " this is "+ element.outerHTML);
});

这里是一个例子:jsFiddle

关于javascript - 元素 $(this) 在 console.log 中输出不同的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35856795/

相关文章:

Firefox 中的 javascript 页面导航错误

Javascript For Loop On Array of Input fields 与 document.getElementById 相关

javascript - 在保持位置时逐渐使用 jQuery

javascript - 当我使用 ES 模块时,为什么变量在浏览器控制台中不可用?

windows - 批处理脚本不再起作用?

java - Struts2中如何保留表单值?

javascript - 使用 JavaScript 将本地镜像转换为 base64

javascript - 这是使用 AngularJS 获取数据到 <li><a href> 的错误吗?

jquery - 属性(对齐)已过时。不鼓励在 HTML5 文档中使用它

javascript - 如何检查哪些功能运行以及何时运行?