admin管理员组

文章数量:1355646

Is there any way to get the value of the aria-label using jQuery?

I have this function

$(document).ready(function() {
    console.log("dom ready")
    $(document).on('click', '.clicker', function(e) {
        e.preventDefault();
        e.stopPropagation();
        var hrefValue = this.href;
        var label = this.aria - label;
        var id = this.id;
        console.log("aria label " + label);
        fire(hrefValue)
    });
});

Which works if I want to get the hrefValue from this dropdown:

<a class="dropdown-toggle rootelements clicker" role="button" aria-label="'+elem.appname+'" data-toggle="dropdown" data-toggle="tooltip" title=' + elem.link + ' href="' + elem.value +'"><i style="color:' + elem.iconcolor + ';" class="' + elem.icon + '" id="jsonicon"></i></a>

But this console.log("aria label " +label) Returns aria label NaN for me when expected output is debug

Is there any way to get the value of the aria-label using jQuery?

I have this function

$(document).ready(function() {
    console.log("dom ready")
    $(document).on('click', '.clicker', function(e) {
        e.preventDefault();
        e.stopPropagation();
        var hrefValue = this.href;
        var label = this.aria - label;
        var id = this.id;
        console.log("aria label " + label);
        fire(hrefValue)
    });
});

Which works if I want to get the hrefValue from this dropdown:

<a class="dropdown-toggle rootelements clicker" role="button" aria-label="'+elem.appname+'" data-toggle="dropdown" data-toggle="tooltip" title=' + elem.link + ' href="' + elem.value +'"><i style="color:' + elem.iconcolor + ';" class="' + elem.icon + '" id="jsonicon"></i></a>

But this console.log("aria label " +label) Returns aria label NaN for me when expected output is debug

Share Improve this question edited Apr 13, 2021 at 14:20 Unmitigated 89.8k12 gold badges99 silver badges104 bronze badges asked Apr 13, 2021 at 11:35 ThomasThomas 951 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

https://api.jquery./attr/

var label = $(this).attr('aria-label');

You can either use camel case or bracket notation since aria-label contains a hyphen, which makes it not a valid identifier, so it cannot be used in dot notation.

var label = this.ariaLabel;
// or
var label = this['aria-label'];

本文标签: javascriptGet arialabel from html using jqueryStack Overflow