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
- 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 thephp.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
3 Answers
Reset to default 2Assuming 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
版权声明:本文标题:javascript - Jquery add selected class to li - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742123542a2421833.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论