简体   繁体   中英

Get aria-label from html using jquery

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

https://api.jquery.com/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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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