Jquery - determining which link was clicked

后端 未结 5 2003
轮回少年
轮回少年 2020-12-20 07:18

I have several similar links that trigger different navigation divs to appear. I am trying to find a way in JQuery to determine which of the links was clicked and then trig

5条回答
  •  情书的邮戳
    2020-12-20 07:48

    Andreas' suggestion is sound - the rel attribute is quite useful for this type of thing, and is the right way to go if you have control over the elements.

    As for your original method, that ought to work as well (the added regex overhead notwithstanding). Just remember that match will return an array or null. If you forgo the null check, you'd use something like:

    var nav_id = $(this).attr('id').match(/\d+/)[0];

    One more tip: specifying the element type in your jQuery selector is good practice for speeding up load times. eg: $('a#nav1') vs $('#nav1'), $('a.expander') vs $('.expander')

提交回复
热议问题