简体   繁体   English

使用html elements属性来查找和更改另一个html元素

[英]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.

相关问题 jQuery按属性定位元素并替换它的HTML - jQuery to locate an element by Attribute and Replace it's HTML 将 HTML 元素属性传递给另一个元素属性 - Pass on HTML element attribute to another element attribute 观察者对元素属性的更改,以更改元素内部的html - Watcher on a element attribute to change the elements inner html 找到属性的值,匹配并找到其他元素上的值,然后更改css - Locate the value of an attribute, match and find the value on other elements and alter the css 在另一个HTML元素中将标题属性显示为字符串 - Display title attribute as string in another HTML element 取得动态元素属性并在另一个元素中使用 - Taking a dynamic elements attribute and using it in another element 通过使用jq的html()或append()将html注入另一个元素,jq无法访问注入的元素,但是使用JS可以吗? - injecting html to another element by html() or append() using jq the injected elements cant be accessed by jq but with JS it works? 使用 Javscript 设置 HTML 元素属性的值等于另一个属性的值 - Setting value of HTML element's attribute equal to another attribute's value using Javscript 如何在下面的HTML中使用Selenium python定位元素 - How to locate the element using selenium python in the HTML below 如何用HTML + CSS中的另一个元素选择器更改元素的属性值? - How to change the attribute value of an element with by another element Selector in HTML + CSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM