使用自定义标记值jquery获取元素id

时间:2022-05-12 09:18:19

How can I get element id using the custom tag value jquery ?

如何使用自定义标记值jquery获取元素ID?

<div id="1" tab_id="4" class="div_focus" tabindex="1">This is label A</div>

how can i get value of id where tab_id = 4 using jquery ?

如何使用jquery获取tab_id = 4的id值?

7 个解决方案

#1


3  

Demo On JsFiddle

try out this selector

试试这个选择器

$('div[tab_id = "4"]').attr("id")

check : Attribute Equals Selector [name="value"]

check:属性等于选择器[name =“value”]

#2


5  

var id = $('[tab_id="4"]').attr('id');

And if there is multiple element with tab_id="4", and you need to get all the ids, then use:

如果有多个元素使用tab_id =“4”,并且您需要获取所有ID,则使用:

var ids = $('[tab_id="4"]').map(function() {
  return this.id;
});

#3


1  

 jQuery('[tab_id="4"]').attr('id')

#4


0  

var id = $('div[tab_id="4"]').prop("id");

#5


0  

use attr()

$('div[tab_id="4"]').attr('id');

#6


0  

Use the attributes selector

使用属性选择器

$('div[tab_id="4"]').prop('id')

or

$('div[tab_id="4"]')[0].id

#7


0  

For Wordpress users, It was tricky for me to figure out!! All I had to do is replace $ with jQuery

对于Wordpress用户来说,弄清楚这对我来说很棘手!!我所要做的就是用jQuery替换$

Working Example:

var ytid = jQuery('div[class="vi-responsive yt-light"]').attr("data-mid");

Wrong Example:

var ytid = $('div[class="vi-responsive yt-light"]').attr("data-mid");

#1


3  

Demo On JsFiddle

try out this selector

试试这个选择器

$('div[tab_id = "4"]').attr("id")

check : Attribute Equals Selector [name="value"]

check:属性等于选择器[name =“value”]

#2


5  

var id = $('[tab_id="4"]').attr('id');

And if there is multiple element with tab_id="4", and you need to get all the ids, then use:

如果有多个元素使用tab_id =“4”,并且您需要获取所有ID,则使用:

var ids = $('[tab_id="4"]').map(function() {
  return this.id;
});

#3


1  

 jQuery('[tab_id="4"]').attr('id')

#4


0  

var id = $('div[tab_id="4"]').prop("id");

#5


0  

use attr()

$('div[tab_id="4"]').attr('id');

#6


0  

Use the attributes selector

使用属性选择器

$('div[tab_id="4"]').prop('id')

or

$('div[tab_id="4"]')[0].id

#7


0  

For Wordpress users, It was tricky for me to figure out!! All I had to do is replace $ with jQuery

对于Wordpress用户来说,弄清楚这对我来说很棘手!!我所要做的就是用jQuery替换$

Working Example:

var ytid = jQuery('div[class="vi-responsive yt-light"]').attr("data-mid");

Wrong Example:

var ytid = $('div[class="vi-responsive yt-light"]').attr("data-mid");