Jquery select all elements that have an accesskey defined

早过忘川 提交于 2019-12-02 03:36:31

问题


I want to select all elements that have an accesskey defined on them.

I know that i can do this: $('[accesskey]'), but that gives me lot of inputs, hrefs etc, on the page (most of them have an empty accesskey).

What is the way to select only the elements where the accesskey actually has a value?

Edit: I found the cause of the empty accesskeys as well, was caused by some old disable / restore accesskeys javascript functions over multiple modal dialogs.. normally you won't get that much elements with empty accesskeys


回答1:


With one selector:

$('[accesskey][accesskey!=""]').foo

How does it work:

// Has the accesskey attribute defined.
[accesskey]

// Doesn't have an empty value for the accesskey attribute.
[accesskey!=""]

Together it select every element which has the accesskey attributes and it's not empty.




回答2:


You can do this

$('[accesskey]').filter(function(){
    return $(this).prop('accessKey');
});

.filter() or like other have already said you can use the attribute-not-equal-selector

Working sample




回答3:


you can use and additional loop

$("[accesskey]").each(function() {
    if($(this)).attr('accesskey').length > 0) {
        // do it
    }
}

i hope it will help you



来源:https://stackoverflow.com/questions/11048015/jquery-select-all-elements-that-have-an-accesskey-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!