简体   繁体   English

锚标记href函数

[英]Anchor Tag href functions

I am trying to create a function that finds the id of an element that an anchor tag directs to, and attach a class to that element.我正在尝试创建一个 function 来查找锚标记指向的元素的 id,并将 class 附加到该元素。 I need this to be vanilla js not jquery.我需要这是香草 js 而不是 jquery。

For Example:例如:

<div id="firstDiv"></div>

<div> Destination </div>
<div> Destination #2 </div>

The idea would be that once you click on the anchor tag that it would take you to the div and attach a class to that div to initiate a function.这个想法是,一旦您单击锚标记,它会将您带到 div 并将 class 附加到该 div 以启动 function。

The script that I've written runs for loops that add the values of the href attribute and the id attribute from javascript objects.我编写的脚本运行循环,从 javascript 对象添加 href 属性和 id 属性的值。 See Below:见下文:

var rooms = [
{
id: 1,
name: Example,
},
{
id:2,
name: Example #2,
}
]

         for ( x in rooms ) {
           var i = document.getElementById("firstDiv");
           var a = document.createElement("a");
           i.appendChild(a);
           a.href = "#" + rooms[x].id;
           a.innerHTML += rooms[x].name + "<br>";
           a.classList.add("show");
          
        }

        var rect = document.getElementsByTagName("rect");
        for ( i = 0; i < rect.length; i++ ) {
           rect[i].setAttribute("id", rooms[i].id);
        } 

Output: Output:

<div id="firstDiv">
<a href="#1"> Example </a>
<a href="#2"> Example #2 </a>
</div>

<div id="1"> Destination </div>
<div id="2"> Destination #2 </div>

The function below is an example of what I want the function to do for each corresponding div when an a tag is clicked.下面的 function 是我希望 function 在单击 a 标签时为每个相应的 div 执行的操作的示例。

function attach() {
var div = document.getElementById(rooms[i].id);
div.classList.toggle("active");
}

Any advice on how to properly write this function for my particular needs.关于如何根据我的特殊需要正确编写此 function 的任何建议。 Would a for loop be best for this or an if/else statement. for 循环是否最适合这个或 if/else 语句。 I've tried both but neither has worked.我都试过了,但都没有奏效。

Please let me know if I need to clarify more.如果我需要澄清更多,请告诉我。

It seems like this is what you are asking about.看来这就是您要问的问题。 See comments inline.见内联评论。

 document.addEventListener("click", function(event){ // First, check to see if it was an anchor that was clicked // in the document if(event.target.nodeName === "A"){ // If so, add a class to the target // Get the href attribute and strip off the "#", then find that element and toggle the class document.getElementById(event.target.getAttribute("href").replace("#","")).classList.toggle("highlight"); } });
 .highlight { background-color:yellow; }
 <div id="firstDiv"> <a href="#one"> Example </a> <a href="#two"> Example #2 </a> </div> <div id="one"> Destination </div> <div id="two"> Destination #2 </div>

Try this:尝试这个:

let anchors = document.querySelectorAll("#firstDiv a");

for (let i = 0; i < anchors.length; i++) {
    anchors[i].addEventListener("click", () => {
        var div = document.getElementById(rooms[i].id);
        div.classList.toggle("active");

} }

You're almost there you just need to add an onclick property for the anchor tag now you can or cannot use the third attach function.I am including both implementations for your reference.你快到了,你只需要为锚标签添加一个 onclick 属性,现在你可以或不能使用第三个附加 function。我包括两个实现供你参考。

for ( x in rooms ) {
       var i = document.getElementById("firstDiv");
       var a = document.createElement("a");
       i.appendChild(a);
       a.href = "#" + rooms[x].id;
       a.innerHTML += rooms[x].name + "<br>";
       a.classList.add("show");
       a.onClick=`document.getElementById(${rooms[x].id}).classList.toggle("active")`;
    }

If you want to have a dedicated function your loop becomes如果你想要一个专用的 function 你的循环变成

for ( x in rooms ) {
       var i = document.getElementById("firstDiv");
       var a = document.createElement("a");
       i.appendChild(a);
       a.href = "#" + rooms[x].id;
       a.innerHTML += rooms[x].name + "<br>";
       a.classList.add("show");
       a.onClick=`attach(${rooms[x].id})`;
    }

Your attach function then becomes:您的附加 function 然后变为:

function attach(id) {
 var div = document.getElementById(id);
 div.classList.toggle("active");
  }

Do let me know if you have any issues.如果您有任何问题,请告诉我。

CSS Only仅限 CSS

This is a bit off-topic but you can use the :target selector to set the style of the corresponding id without JS .这有点跑题了,但是您可以使用:target选择器来设置相应 id 的样式,而无需 JS

 /* element with an id matching the URL fragment */:target { background: gold; } /* you can combine target with other selectors */ #c:target { background: orange } /* just a bit of styling */ a,div { padding: .5rem; display: block; width: 20%; float:left; }
 <a href="#a">Link to A</a> <a href="#b">Link to B</a> <a href="#c">Link to C</a> <a href="#d">Link to D</a> <div id="a">A</div> <div id="b">B</div> <div id="c">C</div> <div id="d">D</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM