2

Heat is my question I have a group of spans just like this

<div class="mydiv">
    <span data-id="0">hello</span>
    <span data-id="1">how</span>
    <span data-id="2">are</span>
    <span data-id="3">you</span>
</div>

using jquery I will get the data id from some where like this

var dataId = 3;

Now I want to add a Class (active) for the span which is having data-id 3 please help me to solve this.

Thanks

4

3 回答 3

9
$('.mydiv span[data-id="3"]').addClass('active');
于 2013-05-27T09:16:27.340 回答
3

演示

$('.mydiv span[data-id="3"]').addClass('highlight');
于 2013-05-27T09:18:43.957 回答
1

尝试这个:

var dataId = 3;    
$("span[data-id=" + dataId + "]").addClass("active");
于 2013-05-27T09:30:50.747 回答