题
我有一个链接列表,这些链接附有点击事件,我需要从孩子那里获得ID链接。因此,在下面的示例中,如果我单击了我需要Google重新调整的第一个列表元素。
我试过了 '$this a'
但是不能完全弄清楚语法。
$("ul li").click(function(event){
$("input").val($(this).html());
});
<ul>
<li><a href="http://www.google.com" id="google">Google</a>
</ul>
解决方案
我看不到样本html,但是
$(this).find('a:first').attr('id')
会这样做(修复 答:首先 选择器如果不是您的意思)
这个 请参阅发射您的活动的元素
其他提示
To make your code a little neater, lets bind triggers with functions like so: (i suggest you give your UL an ID so it is specific to only elements within that UL)
$('ul li').bind('click', getAnchorId);
The function that is called (getAnchorId) gets the ID attribute of the children element (a) of the clicked element (ul li) and applies it to a variable (anchorId), and to show its getting the correct info I put the result in an alert.
function getAnchorId() {
var anchorId = $(this).children('a').attr('id');
alert(anchorId);
}
Now u can do what ever u wish with that variable :)
hope this helps :)
You could use the children
method:
$(this).children('a').eq(0).attr('id');
I'm not sure about the syntax, but something like this should work.