简体   繁体   English

在jQuery多重选择器中使用“ this”

[英]Using “this” in a jquery multiple selector

Im trying to figure this one out, but no joy yet. 我正在设法弄清楚这一点,但还没有高兴。 For reasons that I cant go into here, I need to use both "this" and another selector to bind a click event.. Example 由于我无法进入此处的原因,我需要同时使用“ this”和另一个选择器来绑定click事件。

    $('mySelector').each(function(){
    $(this, this.siblings('img')).bind('click', function(e){
        e.preventDefault();
        doStuffTo($(this));
    });
});

But I cant figure out how to do it. 但是我不知道该怎么做。 Anyone have any idea how I would achieve it? 有人知道我将如何实现吗?

Most of the jQuery methods (that don't return a value) are automatically applied to each element in the collection, so each() is not necessary. 大多数jQuery方法(不返回值)会自动应用于集合中的每个元素,因此each()并不是必需的。

Combining siblings() and andSelf() , the code can be simplified to: 结合siblings()andSelf() ,可以将代码简化为:

$('.mySelector').siblings('img').andSelf().click(function (e) {
    e.preventDefault();
    doStuffTo($(this));
});

Instead, you could do this: 相反,您可以这样做:

$('mySelector').each(function(){
    $(this).siblings('img').andSelf()).bind('click', function(e){
        e.preventDefault();
        doStuffTo($(this));
    });
});

It's not entirely clear what you're trying to do, but perhaps it's this: 尚不清楚您要做什么,但是也许是这样的:

var f = function(e){
    e.preventDefault();
    doStuffTo($(this));
};

$('mySelector').each(function() {
   $(this).bind('click', f);
   $(this).siblings('img').bind('click', f);
});

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

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