jQuery .attr() 方法返回未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11626411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .attr() method returning undefined
提问by Shyantanu
<ul id="footernav">
<li><a href="javascript:void(0);" id="chat" data-icon="custom" data-transition="none">Tools</a></li>
<li><a href="javascript:void(0);" id="email" data-icon="custom" data-transition="none">My Ride</a></li>
<li><a href="javascript:void(0);" id="login" data-icon="custom" data-transition="none">News</a></li>
<li><a href="javascript:void(0);" id="skull" data-icon="custom" data-transition="none">Cool</a></li>
<li><a href="javascript:void(0);" id="coffee" data-icon="custom" data-transition="none" class"ui-btn-active ui-state-persist">Contact</a></li>
</ul>
Now I am using this jQuery function to get the id
of the li
which I have clicked:
现在我正在使用这个 jQuery 函数来获取我点击id
的li
那个:
$('#footernav li').click(function(){
alert($(this).attr('id'));
});
But it returns undefined
.
但它返回undefined
。
回答by zerkms
That's because your li
don't have IDs
那是因为你li
没有身
Use this selector instead: $('#footernav li a')
请改用此选择器: $('#footernav li a')
回答by mas-designs
Because you are on the li element not on the a tag.
因为您在 li 元素上而不是在 a 标签上。
You should listen to
你应该听
$('#footernav li a').click(function(){
alert($(this).attr('id'));
});
回答by rajesh kakawat
This will help you out
这将帮助你
$('#footernav li').click(function(){
alert($('a',this).attr('id'));
});