javascript - Jquery 根据元素的查找方式返回元素的 .width()

标签 javascript jquery arrays

[$('*')[432]]
==>[<a id=​"mini_path_banner" href=​"/​daily-instant-wins-start">​Win over $1,000.00 Instantly​</a>​]

$('#mini_path_banner')
==>[<a id=​"mini_path_banner" href=​"/​daily-instant-wins-start">​Win over $1,000.00 Instantly​</a>​]

[$('*')[432]].width()
==>Uncaught TypeError: undefined is not a function VM5997:2

$('#mini_path_banner').width()
==>300

您可以在这个网站上查看:http://games.pch.com/ 。据我了解, [$('*')[432]]$('#mini_path_banner') 返回两个相同的对象。那么为什么对它们执行相同的方法会得到不同的结果呢?如何找到 $('*')[index] 找到的元素的宽度?

最佳答案

[$('*')[432]].width() 应为 $('*').eq(432).width()

您的情况有两处错误:

  1. $('*')[432] 获取没有 .width() 方法的 DOM 对象。
  2. [$('*')[432]] 是一个单元素数组,其中包含 DOM 对象,但也没有 .width()方法。

使用 .eq(n) 将单个元素放入新的 jQuery 对象(而不仅仅是 DOM 对象本身)中,这样您就可以像 一样调用它的 jQuery 方法。宽度():

$('*').eq(432).width();

关于javascript - Jquery 根据元素的查找方式返回元素的 .width(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27003183/

相关文章:

javascript - 如何将 javascript 输出到 asp.net mvc3 中的 View ?

javascript - jquery preventDefault on alert,如果 alert = ok,则发送

jquery - 使用 jquery 更改 SVG <image> 标签

ios - 如何使用 Swift 动态访问数组中的项目

java - 声明一个数组并更改其内容

javascript - 检索表格单元格值 MVC 5

javascript - 使用 ajax 请求序列化带有 html 值的输入

javascript - 如何用图像标签包裹文本?

jquery - Bootstrap 轮播未加载

python - numpy blit(将数组的一部分复制到另一个具有不同大小的数组)