使用class和data-index属性更改div

时间:2018-04-05 11:44:34

标签: javascript jquery html

 <div class="my_class" data-index=0></div>
 <div class="my_class" data-index=1></div>
 <div class="my_class" data-index=2></div> 
 <div class="my_class" data-index=3></div> 
 <div class="my_class" data-index=4></div>

如何使用jquery更改具有类my_classdata-index=0的div的css属性。

2 个答案:

答案 0 :(得分:3)

查询课程和attribute selector

&#13;
&#13;
$('.my_class[data-index=0]').css('color', 'red');
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="my_class" data-index=0>1</div>
 <div class="my_class" data-index=1>2</div>
 <div class="my_class" data-index=2>3</div> 
 <div class="my_class" data-index=3>4</div> 
 <div class="my_class" data-index=4>5</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
$("[data-index=0]").css('color', 'green');
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="my_class" data-index=0>Test</div>
 <div class="my_class" data-index=1>Test</div>
 <div class="my_class" data-index=2>Test</div> 
 <div class="my_class" data-index=3>Test</div> 
 <div class="my_class" data-index=4>Test</div>
&#13;
&#13;
&#13;