简体   繁体   English

如何获取jQuery数组中单个元素的属性

[英]How to get attributes of individual elements in jquery array

I am getting an array of element in jquery with this $(".drawer-face") 我正在使用$(".drawer-face")在jquery中获取元素数组

If I use $(.drawer-face")[x] I get the x element with .drawer-face returned to me. 如果我使用$(.drawer-face")[x].drawer-face得到x元素,其中包含.drawer-face返回给我。

I want to be able to check attributes within the x element but I get 我希望能够检查x元素内的属性,但是我得到了

Uncaught TypeError: $(...)[6].attr is not a function at <anonymous>:1:22 when I use $(".drawer-face").[6].attr("title") for example. 当我使用$(".drawer-face").[6].attr("title")Uncaught TypeError: $(...)[6].attr is not a function at <anonymous>:1:22 $(".drawer-face").[6].attr("title")例。

$(".drawer-face").eq(x).attr(...)

Use the eq(#) method to keep the element as a jQuery object. 使用eq(#)方法将元素保留为jQuery对象。

 var $drawers = $('.drawer-face'); console.log( $drawers.eq(2).attr('title') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#" class='drawer-face' title="title 1">Title 1</a> <a href="#" class='drawer-face' title="title 2">Title 2</a> <a href="#" class='drawer-face' title="title 3">Title 3</a> <a href="#" class='drawer-face' title="title 4">Title 4</a> <a href="#" class='drawer-face' title="title 5">Title 5</a> 

You can do like this: 您可以这样:

$($(".drawer-face")[6]).attr("title")

$(".drawer-face")[6] gives html dom and jQuery .attr() won't work on that $(".drawer-face")[6]提供html dom和jQuery .attr()不能在


You can use JS and jQuery together like 您可以将JS和jQuery一起使用

$(".drawer-face")[6].getAttribute("title");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM