简体   繁体   English

jQuery获取被单击元素的父级jQuery对象

[英]jQuery get parent jQuery Object of clicked element

My HTML contains many objects like this: 我的HTML包含许多这样的对象:

<div class="class">
  <div class="disconnect_btn">Click here</div>
  <input type="hidden" name="ID" value="12"/>
</div>

I have a function that binds another function to the button element. 我有一个将另一个功能绑定到按钮元素的功能。 Like this: 像这样:

$('.disconnect_btn').bind('click',function(e){
    disconnectFunction(e);
});

What I want to do ist to get first the Div where the button is, I tried to using e.parents('.class') Method, but this always returns error like object has no method... My second target is to extract the value attribute from the hidden input field in parents Div. 我想要做的就是首先获取按钮所在的Div,我尝试使用e.parents('.class')方法,但这总是返回错误,例如对象没有方法...我的第二个目标是提取父Div的隐藏输入字段中的value属性。

function disconnectFunction(e){
    var parent = e.parents('.class'));  //returns error
    var ID = parent.find('input:hidden[name=ID]').attr('value');
    console.log( ID );
}

I also tried this and $(this) selectors, but somehow I got stucked now, maybe there is an easy solution that I can't find. 我也尝试了this和$(this)选择器,但是现在我莫名其妙地陷入了困境,也许有一个我找不到的简单解决方案。

You passing events as argument instead pass DOM. 您将事件作为参数传递,而不是通过DOM。

you can pass it from event by doing it as below, 您可以通过以下方法将其从事件中传递出去,

$('.disconnect_btn').on('click',function(e){
    disconnectFunction(e.target);
});

and in disconnectFunction you can use it as below. 在断开连接功能中,您可以按以下方式使用它。

function disconnectFunction(e){
    var parent = $(e).parents('.class'));  //returns error
             ---^^^^^-----
    var ID = parent.find('input:hidden[name=ID]').attr('value');
    console.log( ID );
}

you need to pass the current object rather than the event object. 您需要传递当前对象而不是事件对象。 and please use on('events') method to attach events to dom elements. 并请使用on('events')方法将事件附加到dom元素。

possibilities 可能性

  1. You can pass current object 您可以传递当前对象
  2. you can access current object in Event object via Event.Target property. 您可以通过Event.Target属性访问Event对象中的当前对象。
  3. You can access this object in your function. 您可以在函数中访问该对象。

     $('.disconnect_btn').on('click',function(e){ disconnectFunction(this); }); function disconnectFunction(currObj){ var parent = $(currObj).parents('.class')); var ID = parent.find('input:hidden[name=ID]').attr('value'); console.log( ID ); } 

or alternativily you can access this object in the function also 或者也可以在函数中访问该对象

function disconnectFunction(e){
        var parent = $(this).parents('.class'));  //here this refer to clicked object
         var parent = $(e.Target).parents('.class'));  //Using event.Target Property
        var ID = parent.find('input:hidden[name=ID]').attr('value');
        console.log( ID );
    }

Your problem is you are trying to use event object, not a HTML object. 您的问题是您尝试使用event对象,而不是HTML对象。

$('.disconnect_btn').bind('click',function(e){
    // `e` refers to event object received by the callback function
    // `this` refers to the HTMLElement that was clicked
    // $(this) turns `this` (which is HTMLElement object) to `jQuery` object
    disconnectFunction($(this)); // this will do the trick
    // now, you can use `e` parameter inside disconnectFunction
    // as a `jQuery` object which has `parents` method defined
});

You just need to do this: 您只需要这样做:

$('.disconnect_btn').on('click',function(e){
    disconnectFunction($(e.target));  // <== Pass $(e.target) or $(this), in place of e
});

And

function disconnectFunction(e){
    var parent = e.parents('.class'); // <== Remove a extra bracket here..  
    var ID = parent.find('input:hidden[name=ID]').val();
    console.log( ID );
}

you use as like that: 你这样使用:

var parent = e.parents('.class'));  //returns error

use as like that: 像这样使用:

var parent = $(e).parents('.class');  

Use e.target to get the clicked element, e is the event object. 使用e.target获取被单击的元素, e是事件对象。 Also you can use .closest() to get the first parent with class class . 您也可以使用.closest()获取class class的第一个父class

Another note: use .val() to get the value of a input field instead of using .attr() 另一个注意事项:使用.val()来获取输入字段的值,而不是使用.attr()

function disconnectFunction(e){
    var parent = $(e.target).closest('.class'));  //returns error
    var ID = parent.find('input:hidden[name=ID]').val();
    console.log( ID );
}

Another solution is to pass the clicked element to disconnectFunction 另一个解决方案是将clicked元素传递给disconnectFunction

function disconnectFunction(el){
    var parent = $(el).closest('.class'));  //returns error
    var ID = parent.find('input:hidden[name=ID]').val();
    console.log( ID );
}

then 然后

$('.disconnect_btn').bind('click',function(e){
    disconnectFunction(this);
});

try this 尝试这个

$('.disconnect_btn').bind('click',function(e){
    disconnectFunction($(this));
});

and

function disconnectFunction(e){
    var parent = $(this).parents('.class');
    var ID = parent.find('input:hidden[name=ID]').attr('value');
    console.log( ID );
}

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

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