i am having "div" which contains span so i want id of that particular span
<div id="status-0" class="abc">
<span id="span_id" class="xyz" title="Enabled"> </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.
If you have data on the div you can use it to narrow the results:
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. 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 . you can use nth-child also instead of 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');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.