Javascript 如何切换属性
我有一个列表项,如果我点击其中任何一个列表项,它会添加一个ID“current” 但是,如果我再次点击这个列表,这个“当前”ID应该被删除并再次恢复正常,比如切换。我可以添加属性,但不能切换。任何帮助都将不胜感激 JSJavascript 如何切换属性,javascript,jquery,Javascript,Jquery,我有一个列表项,如果我点击其中任何一个列表项,它会添加一个ID“current” 但是,如果我再次点击这个列表,这个“当前”ID应该被删除并再次恢复正常,比如切换。我可以添加属性,但不能切换。任何帮助都将不胜感激 JS $('ul li').click(function(){ $('ul li').removeAttr("id","current"); $(this).attr("id","current"); }); 它应该是$('ul li')。removeAttr(“i
$('ul li').click(function(){
$('ul li').removeAttr("id","current");
$(this).attr("id","current");
});
它应该是$('ul li')。removeAttr(“id”)代码>
与此相关的是,最好使用类而不是ID来处理此类内容
更新:
要切换,请使用以下类:
$('ul li').click(function () {
$('ul li').not(this).removeClass("current");
$(this).toggleClass("current");
});
它应该是$('ul li')。删除属性(“id”)代码>
与此相关的是,最好使用类而不是ID来处理此类内容
更新:
要切换,请使用以下类:
$('ul li').click(function () {
$('ul li').not(this).removeClass("current");
$(this).toggleClass("current");
});
以下是如何切换:
$('ul li').click(function(){
$('ul li').not(this).removeAttr("id");
if( $(this).attr('id') == 'current' )
{
$(this).removeAttr('id');
}
else
{
$(this).attr("id","current");
}
});
对于这样的工作,最建议使用css类:
以下是如何切换:
$('ul li').click(function(){
$('ul li').not(this).removeAttr("id");
if( $(this).attr('id') == 'current' )
{
$(this).removeAttr('id');
}
else
{
$(this).attr("id","current");
}
});
对于这样的工作,最建议使用css类:
试试这个:
$('ul li').click(function(){
if($(this).attr("id") == "current"){
$(this).removeAttr("id");
}else{
$(this).attr("id","current");
}
});
试试这个:
$('ul li').click(function(){
if($(this).attr("id") == "current"){
$(this).removeAttr("id");
}else{
$(this).attr("id","current");
}
});
好吧,如果你把它拿走,然后马上把它放回去,当然它永远不会被拿走。还要注意,使用id属性可能不是最好的主意。。。如果您删除一个类,然后立即将其放回原处,那么使用该类会更好,当然,它永远不会被删除。还要注意,使用id属性可能不是最好的主意。。。在我的案例中使用类会更好如果你必须使用ID,那么你可以这样做实际上在我的案例中我需要ID如果你必须使用ID,那么你可以这样做谢谢!这对我来说是准确的!伟大的很高兴我能帮忙。谢谢!这对我来说是准确的!伟大的很高兴我能帮忙。