2010-07-21 3 views

답변

3

어떤 경우이든 값을 반환합니다.

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

.

alert($('div').attr('class')​​​​);​​​​ // will alert sOmEcLaSs 

소문자로 변환하려면 .toLowerCase()을 사용할 수 있습니다.

alert($('div').attr('class').toLowerCase());​​​​ // will alert someclass 

코드 jQuery의 attr 반환 문 (지글 지글되지 않음)에 대한 :

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

또는

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

2

없음을 .attr 때문에 자바 스크립트 .getAttribute 메소드를 호출 ~없이 모든 매개 변수. 코드에서 볼 수 있듯이.

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; 
     }, 
0

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" 
관련 문제