简体   繁体   English

<span>如果我使用jQuery有div</span>的编号,如何获取a的编号<span>?</span>

[英]How do I get a id of a <span> if i have id of div using jQuery?

i am having "div" which contains span so i want id of that particular span 我有包含跨度的 “ div” ,所以我想要该特定跨度的ID

<div id="status-0" class="abc">
    <span id="span_id" class="xyz" title="Enabled">&nbsp;</span>
</div>                                                                                         So how to get id of span with jquery?   and after some action i want to change class of that span using id, how to do that?

你可以做这样的事情

var id = $("div span").attr("id");

You can use this: 您可以使用此:

var id = $('div span').attr('id');

It uses CSS selector(descendant) and apply the attribute function on the matching element. 它使用CSS选择器(后代)并将属性函数应用于匹配的元素。

If you have data on the div you can use it to narrow the results: 如果div上有数据,则可以使用它来缩小结果范围:

var id = $('#status-0 span').attr('id');

Changing class is with: 换班有:

.attr('class', 'value');

Adding class is with: 添加类的方法是:

.addClass('value');

You need to assign the some id / class to div or span to make it more precise selection. 您需要将一些id / class分配给div或span,以使其更精确地进行选择。 If it is not possible you can use parent child selector. 如果不可能,则可以使用父子选择器。

id = $('div > span').attr('id'); // span is direct child

or 要么

id = $('div span').attr('id'); // span is descendant
var spanID = $("#yourdivid span").attr("id") ;

您可以使用该div ID获取跨度的ID

var id = $('#div').find('span').attr('id');

if you know the index of the span under the div better use with eq . 如果您知道div下跨度的索引,最好与eq一起使用。 you can use nth-child also instead of eq . 您也可以使用nth-child代替eq

$('$yourDivID span:eq(<<index>>)').attr('id');

In other case if you have the class of the span you can try the following 在其他情况下,如果您具有跨度的类,则可以尝试以下操作

$('$yourDivID span.spanClass').attr('id');

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

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