简体   繁体   English

在jQuery中选择当前元素

[英]Select current element in jQuery

I have HTML code like this : 我有这样的HTML代码:

<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>

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. 当用户从上面的HTML中单击链接时,我想获取相应<a>元素的jQuery对象,然后操纵它的兄弟。 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. 除了为每个<a>元素创建一个ID,并将该ID传递给onclick事件处理程序之外,我想不出任何其他方法。 I really don't want to use IDs. 我真的不想使用ID。

Any suggestions? 有什么建议?

Fortunately, jQuery selectors allow you much more freedom: 幸运的是,jQuery选择器允许您更自由:

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

...will attach the specified callback to each <a> contained in a <div> . ...将指定的回调附加到<div>包含的每个<a>

When the jQuery click event calls your event handler, it sets "this" to the object that was clicked on. 当jQuery单击事件调用您的事件处理程序时,它会将“this”设置为单击的对象。 To turn it into a jQuery object, just pass it to the "$" function: $(this) . 要将其转换为jQuery对象,只需将其传递给“$”函数: $(this) So, to get, for example, the next sibling element, you would do this inside the click handler: 因此,要获得下一个兄弟元素,您可以在单击处理程序中执行此操作:

var nextSibling = $(this).next();

Edit: After reading Kevin's comment, I realized I might be mistaken about what you want. 编辑:在看完凯文的评论后,我意识到我可能会误解你的想法。 If you want to do what he asked, ie select the corresponding link in the other div, you could use $(this).index() to get the clicked link's position. 如果你想做他要求的事情,即选择另一个div中的相应链接,你可以使用$(this).index()来获得点击链接的位置。 Then you would select the link in the other div by its position, for example with the "eq" method. 然后,您可以通过其位置选择另一个div中的链接,例如使用“eq”方法。

var $clicked = $(this);
var linkIndex = $clicked.index();
$clicked.parent().next().children().eq(linkIndex);

If you want to be able to go both ways, you will need some way of determining which div you are in so you know if you need "next()" or "prev()" after "parent()" 如果您希望能够双向进行,则需要某种方法来确定您所在的div,以便在“parent()”之后需要“next()”或“prev()”

You will find the siblings() and parent() methods useful here. 你会发现siblings()parent()方法在这里很有用。

// assuming A1 is clicked
$('div a').click(function(e) {
    $(this); // A1
    $(this).parent(); // the div containing A1
    $(this).siblings(); // A2 and A3
});

Combining those methods with andSelf() will let you manipulate any combination of those elements you want. 将这些方法与andSelf()组合将允许您操纵所需元素的任意组合。

Edit: The comment left by Mark regarding event delegation on Shog9's answer is a very good one. 编辑:Mark留下的关于Shog9答案的事件授权的评论非常好。 The easiest way to accomplish this in jQuery would be by using the live() method. 在jQuery中完成此操作的最简单方法是使用live()方法。

// assuming A1 is clicked
$('div a').live('click', function(e) {
    $(this); // A1
    $(this).parent(); // the div containing A1
    $(this).siblings(); // A2 and A3
});

I think it actually binds the event to the root element, but the effect is that same. 我认为它实际上将事件绑定到根元素,但效果是一样的。 Not only is it more flexible, it also improves performance in a lot of cases. 它不仅更灵活,而且在很多情况下也提高了性能。 Just be sure to read the documentation to avoid any gotchas. 请务必阅读文档以避免任何问题。

I think by combining .children() with $(this) will return the children of the selected item only 我认为将.children()$(this)结合起来只会返回所选项目的子项

consider the following: 考虑以下:

$("div li").click(function() {
$(this).children().css('background','red');
});

this will change the background of the clicked li only 这将仅更改单击的li的背景

To select the sibling, you'd need something like: 要选择兄弟姐妹,你需要这样的东西:

$(this).next();

So, Shog9's comment is not correct. 所以,Shog9的评论不正确。 First of all, you'd need to name the variable "clicked" outside of the div click function, otherwise, it is lost after the click occurs. 首先,您需要在div单击功能之外命名变量“clicked”,否则,在单击发生后它将丢失。

var clicked;

$("div a").click(function(){
   clicked = $(this).next();
   // Do what you need to do to the newly defined click here
});

// But you can also access the "clicked" element here

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

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