admin管理员组文章数量:1399910
Let's say I have a simple list:
<ul>
<li class="notClicked">1</li>
<li class="notClicked">2</li>
<li class="notClicked">3</li>
</ul>
Can I onClick of one "li" change styles of all li
's except the one clicked?
So if I click "li" number 2, then the list will look like:
<ul>
<li class="notClicked">1</li>
<li class="clicked">2</li>
<li class="notClicked">3</li>
</ul>
So if I click on first "li" then it will have clicked class, while others will be notClicked.
In case you want to play with it, here's jsbin:
Make either in Prototype or plain JavaScript.
Let's say I have a simple list:
<ul>
<li class="notClicked">1</li>
<li class="notClicked">2</li>
<li class="notClicked">3</li>
</ul>
Can I onClick of one "li" change styles of all li
's except the one clicked?
So if I click "li" number 2, then the list will look like:
<ul>
<li class="notClicked">1</li>
<li class="clicked">2</li>
<li class="notClicked">3</li>
</ul>
So if I click on first "li" then it will have clicked class, while others will be notClicked.
In case you want to play with it, here's jsbin: http://jsbin./ucuca4/edit
Make either in Prototype or plain JavaScript.
Share Improve this question edited Apr 15, 2022 at 10:21 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 18, 2010 at 9:46 YanYan 5922 gold badges6 silver badges24 bronze badges3 Answers
Reset to default 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
var Lst;
function CngClass(obj){
if (Lst) Lst.className='';
obj.className='Clicked';
Lst=obj;
}
/*]]>*/
</script>
<style>
.notClicked {color: black}
.Clicked {color: red}
</style>
</head>
<body>
<ul>
<li>
<a onclick="CngClass(this);" href="#" class="notClicked">1
</a>
</li>
<li>
<a onclick="CngClass(this);" href="#" class="notClicked">2
</a>
</li>
<li>
<a onclick="CngClass(this);" href="#" class="notClicked">3
</a>
</li>
</ul>
</body>
</html>
Why change the style of the other? You may want to change the style of the clicked element.
If so, you can use jQuery for that
Example:
<li class = "notClicked">element 1</li>
<li class = "notClicked">element 2</li>
<li class = "notClicked">element 3</li>
$('.notClicked').click(function()
{
$(this).addClass('active');
});
<script>
function changeClass(){
document.getElementById("idElement").setAttribute("class", "Clicked");
}
</script>
<ul>
<li class="notClicked" >1</li>
<li class="notClicked" onClick="changeClass()" id="idElement">2</li>
<li class="notClicked">3</li>
</ul>
本文标签: javascriptonClick change list stylesStack Overflow
版权声明:本文标题:javascript - onClick change list styles - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744127774a2592036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论