2010/06/02 13:43
클래스 이름을 사용하여 요소찾기 개발2010/06/02 13:43
html 에서 아이디 또는 태그이름으로 구성 요소를 찾을 수 있다.
getElementById();
getElementsByTagName();
그러면 클래스 이름으로 요소를 찾을 수 있지 않을까?
검색해보니 getElementsByTagName() 함수가 존재한다.
그런데 파이어폭스 3.6에서는 잘 되지만 IE7.0에서는 이 함수가 존재하지 않는다. (혹시 다른 이름으로 존재할지 모르지)
그래서 사용자 정의 함수로 만들어 보자.
getElementById();
getElementsByTagName();
그러면 클래스 이름으로 요소를 찾을 수 있지 않을까?
검색해보니 getElementsByTagName() 함수가 존재한다.
그런데 파이어폭스 3.6에서는 잘 되지만 IE7.0에서는 이 함수가 존재하지 않는다. (혹시 다른 이름으로 존재할지 모르지)
그래서 사용자 정의 함수로 만들어 보자.
function getElementsByClass(searchClass, node, tag) {
var classElements = new Array();
if ( node == null ) node = document;
if ( tag == null ) tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
for (i=0, j=0; i<elsLen; i++) {
if ( pattern.test(els[i].className))
classElements[j++] = els[i];
}
return classElements;
}
var classElements = new Array();
if ( node == null ) node = document;
if ( tag == null ) tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
for (i=0, j=0; i<elsLen; i++) {
if ( pattern.test(els[i].className))
classElements[j++] = els[i];
}
return classElements;
}
'개발' 카테고리의 다른 글
| 이미지에 워터마크 넣기 (0) | 2010/08/30 |
|---|---|
| 아이폰 웹개발시 팁 (0) | 2010/07/29 |
| 클래스 이름을 사용하여 요소찾기 (0) | 2010/06/02 |
| JSTL 중 루프태그의 상태보기 (varStatus) (0) | 2010/04/15 |
| 스프링을 이용한 GET 데이터 전송시 한글 깨짐 현상 (0) | 2010/04/08 |
| [VIM] .vimrc 이 root 에서만 적용이 안되는 경우 (0) | 2010/02/11 |
TAG 자바스크립트
