在jQuery中选择当前元素 [英] Select current element in jQuery

查看:51
本文介绍了在jQuery中选择当前元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的HTML代码:

I have HTML code like this :

<div>
       <a>Link A1</a>
       <a>Link A2</a>
       <a>Link A3</a>
</div>

<div>
       <a>Link B1</a>
       <a>Link B2</a>
       <a>Link B3</a>
</div>

当用户单击HTML上方的链接时,我想获取相应<a>元素的jQuery对象,然后操纵其同级对象.除了为每个<a>元素创建一个ID并将该ID传递给onclick事件处理程序外,我别无选择.我真的不想使用ID.

When user clicks a link from above HTML, I want to get the jQuery object of the corresponding <a> element, and then manipulate its sibling. I can't think of any way other than creating an ID for each <a> element, and passing that ID to an onclick event handler. I really don't want to use IDs.

有什么建议吗?

推荐答案

幸运的是,jQuery选择器为您提供了更大的自由度:

Fortunately, jQuery selectors allow you much more freedom:

$("div a").click( function(event)
{
   var clicked = $(this); // jQuery wrapper for clicked element
   // ... click-specific code goes here ...
});

...将指定的回调附加到<div>中包含的每个<a>.

...will attach the specified callback to each <a> contained in a <div>.

这篇关于在jQuery中选择当前元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆