简体   繁体   English

触发器单击具有特定类和属性的元素

[英]trigger click an element with a specific class and attribute

I have few link elements related to a certain class and assigned row attributes to it and I'm trying to trigger click event based on the attribute value. 我有几个与某个类相关的链接元素,并为其分配了行属性,我试图根据属性值触发click事件。

 <a class="manage_edit_nb" nb_id="1"></a> | <a class="manage_del_nb" nb_id="1"></a> <a class="manage_edit_nb" nb_id="2"></a> | <a class="manage_del_nb" nb_id="2"></a> <a class="manage_edit_nb" nb_id="3"></a> | <a class="manage_del_nb" nb_id="3"></a>... 

Is it possible to trigger click or any event for a certain attribute value for a certain class? 是否可以针对某个类触发某个属性值的click或任何事件?

If I try something similar to 如果我尝试类似的东西

 $('a[nb_id = "1"]').trigger('click'); 

it triggers click event for all elements irrespective of class but I failed to figure out how to put class reference in there! 它会触发所有元素的click事件而不管是什么类,但我没有弄清楚如何将类引用放在那里!

This should work fine. 这应该工作正常。

$('a.manage_edit_nb[nb_id="1"]').trigger('click');

here it is working: 这是工作:

https://jsfiddle.net/link2twenty/g0txnzfw/ https://jsfiddle.net/link2twenty/g0txnzfw/

First, the nb_id is not a valid attribute, use data-id instead. 首先, nb_id不是有效属性,而是使用data-id data-* attributes are allowed, and I personally like them. 允许data-*属性,我个人喜欢它们。 And, they can be accessed using $.attr('data-id') method, and their value can bee updated using $.attr('data-id', 'new value') . 并且,可以使用$.attr('data-id')方法访问它们,并且可以使用$.attr('data-id', 'new value')更新它们的值。 Going back to the question, try using below selector 回到问题,尝试使用下面的选择器

$('.manage_del_nb[data-id="1"]').get(0).click();

OR 要么

$('.manage_edit_nb[data-id="1"]').get(0).click();

Why .get(0) ? 为什么.get(0) Assuming that the element has been bound with .click(callback()) or .on('click') , the .trigger('click') will not do anything, so I am using .get(0) to get the DOM object which has that method to simulate the click event. 假设元素已经与.click(callback()).on('click') .trigger('click').trigger('click')将不会执行任何操作,因此我使用.get(0)来获取DOM具有该方法来模拟click事件的对象。 Regardless of being said, you can use trigger('click') the way you're already using 无论如何说,你可以按照你已经使用的方式使用trigger('click')

The thing that you are trying to do by the above code is triggering a click event on both '.manage_edit_nb' and '.manage_del_nb' selector and hence the event is occuring on both. 您通过上述代码尝试执行的操作是在'.manage_edit_nb'和'.manage_del_nb'选择器上触发click事件,因此事件在两者上都发生。 Try to be little more specific by giving the class name like 通过给出类名来尝试更具体一些

  $('a.manage_edit_nb[nb_id = "1"]').trigger('click');

I think this is exactly what you need : First of all, change all nb_id to data-nb_id . 我认为这正是您所需要的:首先,将所有nb_id更改为data-nb_id and use the following code, needs jquery. 并使用以下代码,需要jquery。

$(document).ready(function() {
    // Handle click event on the class required 
    $('.manage_edit_nb').click(function(){
        // Get nb_id of that particular anchor event of the class
        var nb_id= $(this).attr('data-nb_id');
        // Switch on nb_id
        switch(nb_id){
            // Handle your cases separately here
            case "1":
            alert('Case 1');break;
            case "2":
            alert('Case 2');break;
            case "3":
            alert('Case 3');break;
        }
    });    
});

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

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