开发者

jQuery check if element contains any attribute

开发者 https://www.devze.com 2022-12-20 06:39 出处:网络
I can check if an element have a specific attribute with: if ($(\'#A\').attr(\'myattr\') !== undefined) {

I can check if an element have a specific attribute with:

if ($('#A').attr('myattr') !== undefined) {
    // attribute exist开发者_运维知识库s
} else {
    // attribute does not exist
}

How can I check if an element have any attribute?

Thank you


If you want to see if the element has a particular attribute, just do this:

if ($('#something').is('[attribute]')) {
  // ...
}


Here's a function that determines whether any of the elements matching a selector have at least one attribute:

function hasOneOrMoreAttributes(selector) {
    var hasAttribute = false;
    $(selector).each(function(index, element) {
        if (element.attributes.length > 0) {
            hasAttribute = true;
            return false; // breaks out of the each once we find an attribute
        }
    });
    return hasAttribute;
}

Usage:

if (hasOneOrMoreAttributes('.someClass')) {
    // Do something
}

If you want to operate on selected elements that have at least one attribute, it's even easier - you create a custom filter:

// Works on the latest versions of Firefox, IE, Safari, and Chrome
// But not IE 6 (for reasons I don't understand)
jQuery.expr[':'].hasAttributes = function(elem) {
    return elem.attributes.length;
};

Which you can use like this:

$(document).ready(function(){
    $('li:hasAttributes').addClass('superImportant');
}


$("selector").get(0).hasAttributes("attributes");


Not a whole jQuery code but it will work

$("a").click(
function () {
    if($("a").get(0).attributes > 0)
        alert("it has attributes");
})


You'd need to use information from Get all Attributes from a HTML element with Javascript/jQuery


I'm not sure if you're wanting to just return the elements that contain the attribute, but if you are, I've found the easiest method to be:

$('selector[attribute]')

That will return the jquery object to include all elements that contain the attribute regardless of its value. For example.

$(':text[maxlength]').addClass('has_max_length');

This would give all text inputs with the maxlength attribute a class of 'has_max_length'.


Here's a solution that I found to be more reliable to know if an element has defined attributes that works in IE, Chrome and Firefox:

$(selector).each(function(index, element) {
    if (element.hasAttributes) { // Some browser supports this method
        if (element.hasAttributes()) {
            return true;
        }
     } else {
           // IE counts many attributes - even if not set explicitly
           var attrs = element.attributes;
           for (var i = 0; i < attrs.length; i++) {
                if (attrs[i].specified) {
                    return true;
                }
            }
     }
     return false;
});

The problem is that IE returns high attributes count even when none is set explicitly. For example, for a simple < p > tag, IE was telling me it had 111 attributes. With that solution, I was able to filter out those attributes that I wasn't interested in.

This solution was taken here: http://help.dottoro.com/ljevmnkf.php (just copied it in case the page is removed!)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号