假设我有以下代码:
<div id="link_other">
<ul>
<li><a href="http://www.google.com/">google</a></li>
<li>
<div class="some_class">
dsalkfnm sladkfm
<a href="http://www.yahoo.com/">yahoo</a>
</div>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,JavaScript将添加target="_blank"到div中的所有链接link_other.
我怎么能用JavaScript做到这一点?
如何删除类名并将其替换为新名称?
<style>
.red {background-color: #FF0000;}
.green{background-color: #00FF00;}
.blue {background-color: #0000FF;}
</style>
<body class="red">
<ul>
<li><a href="">red</a></li>
<li><a href="">green</a></li>
<li><a href="">blue</a></li>
</ul>
</body>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,当您单击红色或绿色或蓝色时,正文类名称将相应更改.它还会制作一个可以节省选择的cookie.
我尝试了jQuery .addClass并且它正在工作,但是它在现有的类之上添加了一个类.此外,我无法保存cookie.