admin管理员组

文章数量:1323723

Hi i have following ul li that display categories name:

<ul>
<li class="selected" data-tab-id="0"></li>
<li data-tab-id="12">...</li>
<li data-tab-id="3">...</li>
<li data-tab-id="15">...</li>
<li data-tab-id="7">...</li>
</ul>

The javascript scripts as follow:

<script type="text/javascript">
var cat_id = '<?=$this->catid?>'

$(document).ready(function () {

});
</script>

Currently the page at index so first li data-tab-id="0" will be selected class also var cat_id will be return nothing. Now when user navigate to another tab such as data-tab-id="12", var cat_id will be return value of 12, how can i remove class selected from default li and replace it to data-tab-id="12". Thanks

Hi i have following ul li that display categories name:

<ul>
<li class="selected" data-tab-id="0"></li>
<li data-tab-id="12">...</li>
<li data-tab-id="3">...</li>
<li data-tab-id="15">...</li>
<li data-tab-id="7">...</li>
</ul>

The javascript scripts as follow:

<script type="text/javascript">
var cat_id = '<?=$this->catid?>'

$(document).ready(function () {

});
</script>

Currently the page at index so first li data-tab-id="0" will be selected class also var cat_id will be return nothing. Now when user navigate to another tab such as data-tab-id="12", var cat_id will be return value of 12, how can i remove class selected from default li and replace it to data-tab-id="12". Thanks

Share Improve this question asked Feb 12, 2013 at 13:23 d3bug3rd3bug3r 2,5863 gold badges36 silver badges75 bronze badges 3
  • im not sure what you mean but this is how you add classes $('li').addClass('selected'); and $('.selected').removeClass('selected'); – iConnor Commented Feb 12, 2013 at 13:25
  • possible duplicate of Selecting element by data attribute – epascarello Commented Feb 12, 2013 at 13:26
  • Just a side note, I noticed you are using PHP by seeing the short tags above in your code. Be mindful of these tags as they are not available on every installation of PHP. It is a setting in the php.ini file that may not be turned on in all installations. This could cause errors when it es to portability of your code. Stick with <?php to ensure patibility across the board. – War10ck Commented Feb 12, 2013 at 13:29
Add a ment  | 

3 Answers 3

Reset to default 2

Assuming list as ID of the UL – just to avoid problem in case you have multiple UL around the page:

$("ul#list")
    .find(".selected").removeClass("selected")
    .end()
    .find("[data-tab-id=" + cat_id + "]").addClass("selected");

You can also do that in two jQuery calls, of course:

$("ul#list .selected").removeClass("selected");
$("ul#list [data-tab-id=" + cat_id + "]").addClass("selected");

use addClass() and removeClass() to add and remove class respectively. try this

updated

var cat_id = '<?=$this->catid?>';
$('li').removeClass('selected'); 
 $('li').each(function(){
   if($(this).attr('data-tab-id')==cat_id){
    $(this).addClass('selected'); 
  }
})

updated without using loop..

var cat_id = '<?=$this->catid?>';
$('li').removeClass('selected');
$('li[data-tab-id='+cat_id+']').addClass('selected');

fiddle here

updated fiddle

use this sample

$('ul li').click(function() {
    $('ul li.selected').removeClass('selected');
    $(this).closest('li').addClass('selected');
})

本文标签: javascriptJquery add selected class to liStack Overflow