简体   繁体   English

通过单击获取元素的ID(JS)

[英]Get id of element by click(JS)

I need to get id of element by click. 我需要通过单击获取元素的id

I tried to write this function 我试图写这个功能

 $('.not-filled-button').click(function() {
    statusFilter();
});


function statusFilter() {

   // var $rows = $('#table tr');
    alert($(this).get(0).id);
}

But when I click button it show undefined in alert 但是当我单击按钮时,它在警报中显示undefined

If I write it like this 如果我这样写

 $('.not-filled-button').click(function() {
    //statusFilter();
    alert($(this).get(0).id);
});

all okay 一切都好

How I can make it work in 1 variant? 如何使它在1个变体中起作用?

The this context inside of statusFilter would be different, statusFilter内部的this上下文会有所不同,

$('.not-filled-button').click(function() {
  statusFilter(this);
});


function statusFilter(_this) {
  alert($(_this).get(0).id);
}

Or you could simply bind the context of the event listener to the function statusFilter 或者您可以简单地将事件侦听器的上下文绑定到功能statusFilter

$('.not-filled-button').click(function() {
  statusFilter.bind(this)(); // Approach 1
  statusFilter.call(this); // Approach 2
  statusFilter.apply(this); // Approach 3
});

And for getting the Id of an element you could simply do, 为了获得元素的ID,您可以轻松地做到这一点,

function statusFilter(_this) {
  alert(_this.id);
}

If your event listener is not going to contain any other function call other than statusFilter , you can just do, 如果您的事件监听器不包含statusFilter以外的任何其他函数调用,则可以执行以下操作:

$('.not-filled-button').click(statusFilter);

You can use call or apply in this case to apply the context to the function called. 在这种情况下,您可以使用callapply来将上下文应用于所调用的函数。 Similar to this: 与此类似:

 $('.not-filled-button').click(function() { statusFilter.apply(this); }); function statusFilter() { // var $rows = $('#table tr'); alert(this.id); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="someButton" class="not-filled-button">click me</button> 


Alternatively you can just pass the element as a parameter, similar to this: 或者,您可以仅将元素作为参数传递,类似于以下内容:

 $('.not-filled-button').click(function() { statusFilter(this); }); function statusFilter(element) { // var $rows = $('#table tr'); alert(element.id); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="someButton" class="not-filled-button">click me</button> 

Or, as mentioned in the comments, if all you ever will call is statusFilter when clicked you can assign the method reference directly, similar to this: 或者,如注释中所述,如果单击时将要调用的只是statusFilter ,则可以直接分配方法引用,类似于以下内容:

 $('.not-filled-button').click(statusFilter); function statusFilter() { // var $rows = $('#table tr'); alert(this.id); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="someButton" class="not-filled-button">click me</button> 

The this context isn't automatically passed into your statusFilter function. this上下文不会自动传递到您的statusFilter函数中。 You could try passing it in as a parameter like this: 您可以尝试将其作为这样的参数传递:

$('.not-filled-button').click(function() {
    statusFilter(this);
});

function statusFilter(elementReference) {
    alert($(elementReference).get(0).id);
}

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

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