繁体   English   中英

如何获取跨度标签的ID

[英]How to Get the ids of span tags

我有一组span标签,例如。

 <span id="q_1" >D1</span>
 <span id="q_2" >D2</span>
 <span id="q_3" >D3</span>

我如何在jquery的帮助下获取span标签的ID。数字1,2,3是在运行时生成的,所以我拥有的基本结构是

<span id="q_" ></span>
$("span").each(function(){
     var thisId = $(this).attr('id');
     // Do whatever you want with the Id, and go on to the next one.
});

:)

$('span').each(function(){
   alert($(this).attr('id'));
});

我觉得您实际上是在问如何选择ID以“ q_”开头的所有元素。 如果是这样,最简单的方法是这样的:

var qSpans = $('span[id^="q_"]');

参见示例→

获得一个数组,如果ID:

var a = $.map($('span'), function(s){ return s.id; });
$('span');

会找到所有的跨度。

$('span').filter(function() {
    return (/^q\_\d+$/i).test($(this).attr('id'));
});

或仅跨度ID匹配格式的人

暂无
暂无

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

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