JQuery.attr () lower case guarantee?

Does it guarantee $(selector).attr(name)that the result is lowercase if the attribute is found?

+5
source share
3 answers

It will return the value anyway when it was installed.

<div class="sOmEcLaSs">content</div>

.

alert( $('div').attr('class')โ€‹โ€‹โ€‹โ€‹ );โ€‹โ€‹โ€‹โ€‹  // will alert sOmEcLaSs

If you want to convert to lowercase, you can use .toLowerCase().

alert( $('div').attr('class').toLowerCase() );โ€‹โ€‹โ€‹โ€‹  // will alert someclass

Code for jQuery attrreturn statements (not Sizzle):

http://github.com/jquery/jquery/blob/1.4.2/src/attributes.js#L308

or

http://github.com/jquery/jquery/blob/1.4.2/src/attributes.js#L325

+3
source

No, because .attr calls the javascript.getAttribute method without any parameters. As you can see in the code below.

getAttribute - 0, , , .

     ATTR: function(elem, match){
            var name = match[1],
                result = Expr.attrHandle[ name ] ?
                    Expr.attrHandle[ name ]( elem ) :
                    elem[ name ] != null ?
                        elem[ name ] :
                        elem.getAttribute( name ),
                value = result + "",
                type = match[2],
                check = match[4];

            return result == null ?
                type === "!=" :
                type === "=" ?
                value === check :
                type === "*=" ?
                value.indexOf(check) >= 0 :
                type === "~=" ?
                (" " + value + " ").indexOf(check) >= 0 :
                !check ?
                value && result !== false :
                type === "!=" ?
                value !== check :
                type === "^=" ?
                value.indexOf(check) === 0 :
                type === "$=" ?
                value.substr(value.length - check.length) === check :
                type === "|=" ?
                value === check || value.substr(0, check.length + 1) === check + "-" :
                false;
        },
+2

jQuery , , - . IE DOM , ; <div id="mydiv"> <DIV ID=mydiv>. , Netscape Firefox id, IE id. , , IE. , IE6 IE8 - getAttribute(). :

<div></div>

var myDiv = document.getElementsByTagName('div')[0];
myDiv.setAttribute('id','id1');
myDiv.setAttribute('ID','id2');
console.log(x.getAttribute('ID')); // IE6, return "id1", IE8, returns "id2"
console.log(x.getAttribute('ID',true)); // IE6, return "id2", returns "id2"
0

All Articles