[英]Using html elements attribute to locate and alter another html element
I would like to append a active class to the particular div that is referenced by hashtag. 我想将活动类添加到由hashtag引用的特定div上。
Ex: 例如:
<a href="#cont1">content1</a>
<a href="#cont2">content2</a>
<a href="#cont3">content3</a>
<div id="cont1">lorem ipsum dummy content</div>
<div id="cont1">lorem ipsum dummy content</div>
<div id="cont1">lorem ipsum dummy content</div>
I would like to append one class to each div on hyperlink click.I mean if i click the anchor tag with href "#cont1" , one class name needs to append for div id "cont1". 我想在超链接单击上为每个div附加一个类。我的意思是,如果我单击带有href“#cont1”的锚标记,则需要为div id“ cont1”附加一个类名称。
Any help on this will be appreciated. 任何帮助,将不胜感激。
id should be unique and you can't add the same id for div use class instead id应该是唯一的,您不能为div use类添加相同的id
<a onclick="test()">content1</a>
<div class="cont1">lorem ipsum dummy content</div>
//this is the javascript
function test(){
$(".cont1").addClass('active');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.