jQuery: How to get the value of an html attribute?

前端 未结 6 890
一生所求
一生所求 2020-11-30 13:01

I\'ve got an html anchor element:

Link Text

...and I want to get the contents of the title so

相关标签:
6条回答
  • $('a').click(function() {
        var title = $(this).attr('title');
        alert(title);
    });
    
    0 讨论(0)
  • 2020-11-30 13:39

    You can simply use this.title inside the function

    $('a').click(function() {
        var myTitle = $(this).attr ( "title" ); // from jQuery object
        //var myTitle = this.title; //javascript object
        alert(myTitle);
    });
    

    Note

    Use another variable name instead of 'alert'. Alert is a javascript function and don't use it as a variable name

    0 讨论(0)
  • 2020-11-30 13:40
    $('a').click(function() {
        var title = $(this).attr('title');
        alert(title);
    });
    
    0 讨论(0)
  • 2020-11-30 13:40

    Even you can try this, if you want to capture every click on the document and get attribute value:

    $(document).click(function(event){
        var value = $(event.target).attr('id');
        alert(value);
    });
    
    0 讨论(0)
  • 2020-11-30 13:48
    $(this).attr("title")
    
    0 讨论(0)
  • 2020-11-30 13:50

    You can create function and pass this function from onclick event

    <a onclick="getTitle(this);" title="Some stuff here">Link Text</a>
    
    <script type="text/javascript">
    
    function getTitle(el)
    {
         title = $(el).attr('title');
         alert(title);
    }
    
    </script>
    
    0 讨论(0)
提交回复
热议问题