简体   繁体   English

为什么jQuery attr返回undefined而不是'href'?

[英]Why is jQuery attr returning undefined for 'id' but not 'href'?

I am trying to get the id from li elements using jQuery attr('id'), but it is returning undefined even though it does return what appears to be the correct values for attr('href'). 我正在尝试使用jQuery attr('id')从li元素获取ID,但是即使返回的确是attr('href')的正确值,它也会返回未定义的值。 I'm stumped. 我很沮丧 Here's a link to my Codepen 这是我的Codepen 的链接

  <ul id="sortList">
  <li id="one"><a href='#'>Ratings</a></li>
  <li id="2"><a href=#>Reviews</a></li>
  <li id="3"><a href=#>Type</a></li>
  </ul>
  <h2 id="anchorInfo">hello</h2>
  <h2 id="clickInfo">hello</h2>


  $('#sortList li').click( function(event){
  event.preventDefault();
  let whatHref = $(event.target).attr('href');
  let whatId = $(event.target).attr('id');
  document.getElementById('anchorInfo').innerHTML = whatHref;
  document.getElementById('clickInfo').innerHTML = whatId;
  })

Instead of 代替

  $(event.target).attr('id');

try 尝试

  $(this).attr('id');

The event.target is the link, and that doesn't have an id . event.target是链接,并且没有id You need to get it from the parent: 您需要从父母那里得到它:

$(event.target).parent().attr('id')

First you need to target each of the individual elements using the jQuery .each() function. 首先,您需要使用jQuery .each()函数来定位每个单独的元素。 You will then be able to target the clicked elements by using the 'this' selector. 然后,您可以使用“此”选择器来定位点击的元素。 Finally to target the anchor you need to find the child element of the clicked li by using the jQuery .find() function. 最后,要定位到锚点,您需要使用jQuery .find()函数找到单击的li的子元素。 You should then be good to go by friend! 这样,您应该可以和朋友一起去! Codepen Codepen

Reviewed and tested jQuery code: 经检查和测试的jQuery代码:

$('#sortList li').each(function(){
  $(this).click(  function(event){
    event.preventDefault();
    let whatHref = $(this).find('a').attr('href');
    let whatId = $(this).attr('id');
    document.getElementById('anchorInfo').innerHTML   = whatHref;
    document.getElementById('clickInfo').innerHTML   = whatId;
  });
});

You should use jQuery each function. 您应该使用jQuery每个函数。 Then, instead of using this reference, you should use item as parameter from which you may do things treating it as HTMLLIElement . 然后,您应该使用item作为参数,而不是使用引用,您可以通过参数将其视为HTMLLIElement

To use .attr you need pass a data-attribute like data-id="something" . 要使用.attr您需要传递data-attribute例如data-id="something" Try use .prop() 尝试使用.prop()

  <ul id="sortList">
  <li id="one"><a href='#'>Ratings</a></li>
  <li id="2"><a href=#>Reviews</a></li>
  <li id="3"><a href=#>Type</a></li>
  </ul>
  <h2 id="anchorInfo">hello</h2>
  <h2 id="clickInfo">hello</h2>


  $('#sortList li').click( function(event){
  event.preventDefault();
  let whatHref = $(event.target).prop('href');
  let whatId = $(event.target).prop('id');
  document.getElementById('anchorInfo').innerHTML = whatHref;
  document.getElementById('clickInfo').innerHTML = whatId;
  })

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

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