简体   繁体   English

<ul><li>列出添加onclick的项目

[英]<ul> <li> list items adding onclick

I'm trying to add onclick to my list items and get the href with pure JavaScript. 我试图将onclick添加到列表项中,并使用纯JavaScript获得href。 I had it working from just a div but as soon as I added list items I was unable to get it to work. 我只有一个div才能工作,但是一旦添加列表项,我就无法使其工作。

 var navBarDiv = document.getElementById("navbar"); var contentDiv = document.getElementById("content"); var navBarList = navBarDiv.getElementsByTagName("ul"); // Attach click listeners for each of the nav links. for (var i = 0; i < navBarList.length; i++) { var navBarItems = navBarList[i].getElementsByTagName("li"); for (var j = 0; j < navBarItems.length; j++) { navBarItems[j].addEventListener('click', function (e) { e.preventDefault(); var pageURL = this.attributes['href'].value; GetFileDoc(pageURL, function (doc) { contentDiv.innerHTML = doc.getElementById("content").innerHTML; }); }); } } 
 <div id="navbar"> <ul> <li><a href="\\" class="active">Homepage</a></li> <li><a href="pages/about.html">About Us</a></li> <li><a href="#">Gallery</a></li> <li><a href="pages/contact.html">Contact Us</a></li> </ul> </div> 

This appears to be the problem: 这似乎是问题所在:

var pageURL = this.attributes['href'].value;

You're adding the click handler to the li element, not the a element. 您要将点击处理程序添加到li元素,而不是a元素。 So there is no href attribute. 因此,没有href属性。 I imagine there are two options: 我想有两种选择:

  1. Drill into the element further to get the href of the child a element, or 钻入元件进一步得到href孩子的a元素,或
  2. Add the click handler to the a element instead 将点击处理程序添加到a元素中

For the first option, you might do something like this instead: 对于第一种选择,您可以改为执行以下操作:

var pageURL = this.getElementsByTagName("a")[0].attributes['href'].value;

You'd want to put in some error checking of course, unless you're very confident that there will always be that first a element that you want. 当然,您将希望进行一些错误检查,除非您非常有信心总是会有您想要的第a元素。

For the second option, you might just modify what you're looping over. 对于第二个选项,您可以只修改要循环的内容。 Perhaps something like this: 也许是这样的:

var navBarItems = navBarList[i].getElementsByTagName("a");

Assuming there are no other a elements in the hierarchy that you don't want to use for this. 假设层次结构中没有其他您不想使用a元素。

Try with 试试看

  1. a tag is the children of li so you need use querySelector for target the child element a标签是在孩子li ,所以你需要使用querySelector为目标的子元素
  2. And simply use with element.href for getting href value 只需与element.href一起使用以获得href值

 var navBarDiv = document.getElementById("navbar"); var contentDiv = document.getElementById("content"); var navBarList = navBarDiv.getElementsByTagName("ul"); for (var i = 0; i < navBarList.length; i++) { var navBarItems = navBarList[i].getElementsByTagName("li"); for (var j = 0; j < navBarItems.length; j++) { navBarItems[j].addEventListener('click', function(e) { e.preventDefault(); var pageURL = this.querySelector('a').href; console.log(pageURL) //GetFileDoc(pageURL, function(doc) { //contentDiv.innerHTML = doc.getElementById("content").innerHTML; // }); }); } } 
 <div id="navbar"> <ul> <li><a href="\\" class="active">Homepage</a></li> <li><a href="pages/about.html">About Us</a></li> <li><a href="#">Gallery</a></li> <li><a href="pages/contact.html">Contact Us</a></li> </ul> </div> 

You are adding event to each li in the list. 您正在将事件添加到列表中的每个li。 According to the rest of the code, espscialy the line this.attributes['href'].value you expect to have link element loaded. 根据其余代码,特别是您希望已加载链接元素的行this.attributes['href'].value You can load all the link elements instead right away by using 您可以使用以下方法立即加载所有链接元素

navBarList[i].querySelectorAll("a");

which gives you all the inner links. 这给了你所有的内在联系。

Try this one: 试试这个:

var navBarDiv = document.getElementById("navbar");
var contentDiv = document.getElementById("content");

var navBarLinks = navBarDiv.getElementsByTagName("a");

// Attach click listeners for each of the nav links.
for (var i = 0; i < navBarLinks.length; i++) {


    navBarLinks[i].addEventListener('click', function (e) {
        e.preventDefault();

        var pageURL = this.attributes['href'].value;

        GetFileDoc(pageURL, function (doc) {
            contentDiv.innerHTML = doc.getElementById("content").innerHTML;
        });
    });
}

} }

It's nearly the same except I filter directly to the anchor-tags and then there is a href attribute. 几乎一样,除了我直接过滤到锚标记,然后有一个href属性。

Without using ES6 不使用ES6

 Array.prototype.slice.call(document.querySelectorAll('#navbar ul li a')).map(function(item) { item.addEventListener('click', function(e) { e.preventDefault(); console.log("href", e.target.href); }); }); 
 <div id="navbar"> <ul> <li><a href="\\" class="active">Homepage</a></li> <li><a href="pages/about.html">About Us</a></li> <li><a href="#">Gallery</a></li> <li><a href="pages/contact.html">Contact Us</a></li> </ul> </div> 

Using ES6 使用ES6

 [...document.querySelectorAll('#navbar ul li a')] .map((item) => { item.addEventListener('click', (e) => { e.preventDefault(); console.log("href", e.target.href); }); }); 
 <div id="navbar"> <ul> <li><a href="\\" class="active">Homepage</a></li> <li><a href="pages/about.html">About Us</a></li> <li><a href="#">Gallery</a></li> <li><a href="pages/contact.html">Contact Us</a></li> </ul> </div> 

jQuery jQuery的

 jQuery('#navbar ul li').click(function() { console.log("href", $(this).attr('href')) }); 

Please try this code 请尝试此代码

  var navBarDiv = document.getElementById("navbar");
  var contentDiv = document.getElementById("content");

  var navBarList = navBarDiv.getElementsByTagName("ul");

  // Attach click listeners for each of the nav links.
  for (var i = 0; i < navBarList.length; i++) {

      var navBarItems = navBarList[i].getElementsByTagName("li");
      for (var j = 0; j < navBarItems.length; j++) {

          navBarItems[j].addEventListener('click', function (e) {
              e.preventDefault();
                        var pageURL = this.getElementsByTagName('a')[0].href;
              alert(pageURL);
               GetFileDoc(pageURL, function (doc) {
                 contentDiv.innerHTML = doc.getElementById("content").innerHTML;
               });
          });
      }
  }

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

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