简体   繁体   English

如何<a>在文本“示例”之后的 javascript 中</a>触发<a>标记事件</a>

[英]How to fire the <a> tag event in javascript that comes after the text "Example"

I need to fire an javascript event when anchor tags, after a certain text, is clicked.当单击某个文本后的锚标记时,我需要触发一个 javascript 事件。 My page contains lot of anchor tags and i need to fire an event only when the anchor tags below the text "example" is clicked without using any class name or ids.我的页面包含很多锚标记,只有在不使用任何类名或 ID 的情况下单击文本“示例”下方的锚标记时,我才需要触发事件。 Is it possible?是否可以?

If you can add any id or class to your example article you can add the event listener for that in JavaScript:如果您可以在示例文章中添加任何idclass ,则可以在 JavaScript 中为其添加事件侦听器:

<article>ABC</article>
<a></a>
<article class='exampleArticle'>Example</article>
<a></a>
<a></a>
<article>ABC</article>

<script>

 var ele = document.querySelector('.exampleArticle')
 // For first tag
 ele.nextElementSibling.addEventListener('click', function(ele) {
  //Do any thing here
 }
// For second tag
ele.nextElementSibling.nextElementSibling.addEventListener('click', function(ele) {
  //Do any thing here
 }



</script>

If I'm correctly understanding what you mean, you can set event listeners by using an id for each anchor tag eg如果我正确理解您的意思,您可以通过为每个锚标记使用 id 来设置事件侦听器,例如

<article>ABC</article>
<a></a>
<article>Example</article>
<a id="fire-event1" href="#">Anchor1</a>
<a id="fire-event2" href="#">Anchor2</a>
<article>ABC</article>

<script>
  var fireEvent1 = document.getElementById("fire-event1");
  var fireEvent2 = document.getElementById("fire-event2");

  fireEvent1.addEventListener("click", myFunction1);
  fireEvent2.addEventListener("click", myFunction2);

  function myFunction1() {
    alert("Event 1 activated");
  }
  function myFunction2() {
    alert("Event 2 activated");
  }
</script>

or, if you want several anchors to fire with the same function, allocate the target anchors to a class and then cycle through them to set the listener on each one eg或者,如果您希望多个锚点使用相同的功能触发,请将目标锚点分配给一个类,然后循环遍历它们以在每个锚点上设置侦听器,例如

<article>ABC</article>
<a></a>
<article>Example</article>
<a class="fire-event" href="#">Anchor1</a>
<a class="fire-event" href="#">Anchor2</a>
<article>ABC</article>

<script>
  var fireEvent = document.getElementsByClassName("fire-event");

  for (var i = 0; i < fireEvent.length; i++) {
    fireEvent[i].addEventListener('click', myFunction);
  }

  function myFunction() {
    alert("Event fired");
  }
</script>

JSFiddle JSFiddle

Vaibhav Kumar's answer will work with the given example, but note that it has syntax errors because he has not closed the parentheses on the addEventListener statements. Vaibhav Kumar 的回答适用于给定的示例,但请注意,它存在语法错误,因为他没有关闭 addEventListener 语句上的括号。 They should be:他们应该是:

ele.nextElementSibling.addEventListener('click', function(ele) {
  //Do any thing here
});

It's also going to get very messy if you want to add the listener to a lot of HTML tags - soon it's going to look like...如果您想将侦听器添加到许多 HTML 标记中,它也会变得非常混乱 - 很快它就会看起来像......

ele.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.addEventListener('click', function(ele) {
  //Do any thing here
});

To set a listener to trigger the same function on a number of HTML tags, just allocate a class to the tags you want to target;要设置侦听器以在多个 HTML 标签上触发相同的功能,只需为要定位的标签分配一个类即可; for triggering separate functions, use id or classes.要触发单独的功能,请使用 id 或 classes。 See my full answer with example code.用示例代码查看我的完整答案。

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

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