.removeAttr()
원문 링크
http://api.jquery.com/removeAttr/
개요 : 조건에 맞는 요소 집합에서 각 요소의 속성을 제거합니다.
- .removeAttr( attributeName )
- attributeName 제거하기 위한 속성
.removeAttr() 함수는 JavaScript removeAttribute() 함수를 사용합니다. 하지만 .removeAttr() jQuery 객체를 바로 사용할 수 있고 다른 브라우져간 속성명이 상이함을 해소하는 부분과 같은 것에서 좀 더 유리한 점이 있습니다.
Note: onclick 이벤트 핸들러에 .removeAttr() 함수를 사용하여 제거하려 한다면 Internet Explorer 6, 7, 8 별로 다른 작동이 나타나기도 합니다. 그런 가능성을 피하기 위해서는 .prop() 을 대신 사용하세요.
$element.prop("onclick", null);
console.log("onclick property: ", $element[0].onclick);
.removeAttr() 함수는 앞으로도 더 개선되어야 합니다. 당분간은 .prop("onclick", null) 와 같이 사용해서 크로스 브라우징(cross-browser)을 지원하시는 것을 권고합니다.
예 제
버튼을 클릭해서 속성을 제거합니다.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button>Enable</button>
<input type="text" title="hello there" />
<div id="log"></div>
<script>
(function() {
var inputTitle = $("input").attr("title");
$("button").click(function () {
var input = $(this).next();
if ( input.attr("title") == inputTitle ) {
input.removeAttr("title")
} else {
input.attr("title", inputTitle);
}
$("#log").html( "input title is now " + input.attr("title") );
});
})();
</script>
</body>
</html>
미리보기
버튼 클릭해 보셔요. input의 title값을 토글해 주는 예제네요.
그럼 즐프하세요.
※ 본 예제는 http://www.jquery.com 에 있는 내용임을 밝힙니다.
'프로그래밍 > jQuery' 카테고리의 다른 글
| jQuery API 정복 - 요소 바꾸기, replaceWith() (0) | 2011.11.22 |
|---|---|
| jQuery API 정복 - 요소 바꾸기, replaceAll() (2) | 2011.11.22 |
| jQuery API 정복 - property 제거, removeProp() (0) | 2011.11.22 |
| jQuery API 정복 - 클래스 제거, removeClass() (1) | 2011.11.22 |
| jQuery API 정복 - 요소 제거, remove() (0) | 2011.11.22 |
| jQuery API 정복 - 선택된 모든 요소의 앞에 추가하기2, prependTo() (0) | 2011.11.22 |
| jQuery API 정복 - 선택된 모든 요소의 앞에 추가하기, prepend() (0) | 2011.11.22 |
| jQuery API 정복 - 상대 좌표 구하기, position() (0) | 2011.11.22 |