简体   繁体   English

如何获取具有div ID的li的onclick事件

[英]how to get onclick event of li with a div id

if the code snippet is like this: 如果代码片段是这样的:

<div id="test">

<div>
<h3>First section</h3>
 <ul>
  <li><a href="http://example.com">Item 1</a></li>
  <li><a href="http://example.com">Item 2</a></li>
  <li><a href="http://example.com">Item 3</a></li>
 </ul>
</div>

<div>
<h3>Second section</h3>
<ul>
  <li><a href="http://example.com">Item 4</a></li>
  <li><a href="http://example.com">Item 5</a></li>
  <li><a href="http://example.com">Item 6</a></li>
 </ul>
</div>

</div>

I have the id "test" of the first div. 我有第一个div的ID“测试”。 how can i get the innerhtml of clicked items. 我如何获取被单击项的innerhtml like alert the answer "item 1" when <li><a href="http://example.com">Item 1</a></li> is clicked. 例如当点击<li><a href="http://example.com">Item 1</a></li>时,提示答案“项目1”。 I already tried this following code 我已经尝试过以下代码

var ul = document.getElementById("test").getElementsByTagName('ul')[0];
ul.onclick = function(event) {
event = event || window.event;
var target = event.target || event.srcElement;
alert(target.getAttribute("href"));
};

This working good only for the first section ul. 此工作仅适用于第一部分ul。 i need the same to happen for N uls. 我需要同样的情况发生N个 ULS。 What will be the best method for this. 什么是最好的方法。 Thanks in advance. 提前致谢。

Note: In my project i can't use jquery. 注意:在我的项目中,我无法使用jquery。 only javascript or YUI is supported 仅支持javascript或YUI

Iterate over each ul and add the click handler to it like 遍历每个ul并向其添加点击处理程序

 var uls = document.getElementById("test").getElementsByTagName('ul'); for (var i = 0; i < uls.length; i++) { uls[i].addEventListener('click', clickHandler) } function clickHandler(event) { event = event || window.event; var target = event.target || event.srcElement; alert(target.getAttribute("href")); } 
 <div id="test"> <div> <h3>First section</h3> <ul> <li><a href="http://example.com">Item 1</a></li> <li><a href="http://example.com">Item 2</a></li> <li><a href="http://example.com">Item 3</a></li> </ul> </div> <div> <h3>Second section</h3> <ul> <li><a href="http://example.com">Item 4</a></li> <li><a href="http://example.com">Item 5</a></li> <li><a href="http://example.com">Item 6</a></li> </ul> </div> </div> 

You can use Event Delegation to achieve the expected result. 您可以使用事件委托来达到预期的结果。

  1. Bind a single event handler to the parent, and get the child from event.target 将单个事件处理程序绑定到父级,然后从event.target获得子级

 //Get the object of the div#test var testDiv = document.getElementById('test'); //Bind onclick event for the div#test object testDiv.onclick = function(e) { //Prevent the default href action (optional) e.preventDefault(); //Check whether the target element is an Anchor tag if (e.target.nodeName == 'A') { alert(e.target.innerHTML); } } 
 <div id="test"> <div> <h3>First section</h3> <ul> <li><a href="http://example.com">Item 1</a> </li> <li><a href="http://example.com">Item 2</a> </li> <li><a href="http://example.com">Item 3</a> </li> </ul> </div> <div> <h3>Second section</h3> <ul> <li><a href="http://example.com">Item 4</a> </li> <li><a href="http://example.com">Item 5</a> </li> <li><a href="http://example.com">Item 6</a> </li> </ul> </div> </div> 

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

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