jQuery jquery单击并获取href属性的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6625667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:08:57  来源:igfitidea点击:

jquery click & get value of attributes of a href

jqueryattributesclicklistener

提问by bob

How can I access seq value in a href, when I click thye link?

当我单击 thye 链接时,如何访问 href 中的 seq 值?

/* Add a listner to Group buttons */
    $('a.preActNav').click(function() { 
        alert(this.seq)
    });   



    <li><a href="#preclose4" data-theme="a" data-icon="arrow-d" data-transition="none" seq='1' class="preActNav"  ID="preActNavA">A</a></li>
    <li><a href="#preclose4" data-theme="a" data-icon="arrow-d" data-transition="none" seq='2' class="preActNav"  ID="preActNavB">B</a></li>

回答by Naftali aka Neal

Like this:

像这样:

alert($(this).attr('seq'));


Although it may be better to put seqas another data element:

尽管将其seq作为另一个数据元素可能会更好:

<li><a href="#preclose4" data-theme="a" data-icon="arrow-d" data-transition="none" data-seq='1' class="preActNav"  ID="preActNavA">A</a></li>

So then you can do:

那么你可以这样做:

alert($(this).data('seq'));


回答by kungphu

Have you tried: $(this).attr()? http://api.jquery.com/attr/

你有没有试过:$(this).attr()?http://api.jquery.com/attr/

回答by biluriuday

try the following

尝试以下

$('a.preActNav').click(function() {
   alert($(this).attr('seq'));
});

you need to use $(this) jquery selector instead

您需要改用 $(this) jquery 选择器

回答by ShankarSangoli

Try this

尝试这个

$('a.preActNav').click(function() { 
        alert($(this).attr('seq'));
        //To get href
        alert(this.href);
    });   

回答by Rashid Iqbal

you can either use this code:

您可以使用此代码:

 $('.item').click(function(){

        console.log($(this).attr("name"));

    });

or

或者

$('.item').on('click', () => {

    console.log($(".item.active").attr("name"));

});