jQuery: 선택한 요소 태그 이름을 가져옵니다.
태그 이름을 쉽게 얻을 수 있는 방법이 있나요?
예를 들어, 만약 내가$('a')
함수로 만들고 싶다'a'
.
전화하시면 됩니다..prop("tagName")
. 예:
jQuery("<a>").prop("tagName"); //==> "A"
jQuery("<h1>").prop("tagName"); //==> "H1"
jQuery("<coolTagName999>").prop("tagName"); //==> "COOLTAGNAME999"
기입하는 경우.prop("tagName")
번거롭기 때문에 다음과 같은 커스텀 함수를 작성할 수 있습니다.
jQuery.fn.tagName = function() {
return this.prop("tagName");
};
예:
jQuery("<a>").tagName(); //==> "A"
jQuery("<h1>").tagName(); //==> "H1"
jQuery("<coolTagName999>").tagName(); //==> "COOLTAGNAME999"
태그명은 관례상 대문자로 반환됩니다.반환된 태그 이름을 모두 소문자로 하려면 다음과 같이 커스텀 함수를 편집할 수 있습니다.
jQuery.fn.tagNameLowerCase = function() {
return this.prop("tagName").toLowerCase();
};
예:
jQuery("<a>").tagNameLowerCase(); //==> "a"
jQuery("<h1>").tagNameLowerCase(); //==> "h1"
jQuery("<coolTagName999>").tagNameLowerCase(); //==> "cooltagname999"
DOM 의 속성을 사용할 수 있습니다.
$(...)[0].nodeName
jQuery 1.6부터는 프로포즈를 호출해야 합니다.
$target.prop("tagName")
http://api.jquery.com/prop/ 를 참조해 주세요.
jQuery 1.6 이상
jQuery('selector').prop("tagName").toLowerCase()
이전 버전
jQuery('selector').attr("tagName").toLowerCase()
toLowerCase()
는필수가아닙니다.
이것은 또 다른 방법입니다.
$('selector')[0].tagName
사용해서는 안 됩니다.jQuery('selector').attr("tagName").toLowerCase()
이전 버전의 Jquery에서만 작동하기 때문입니다.
사용할 수 있습니다.$('selector').prop("tagName").toLowerCase()
> = 버전 1.6의 jQuery 버전을 사용하는 것이 확실한 경우.
주의:
지금(2016년 1월)에는 모두 jQuery 1.10+ 같은 것을 사용하고 있다고 생각하실 수 있습니다만, 유감스럽게도 그렇지 않습니다.예를 들어, 오늘날 많은 사람들이 여전히 Drupal 7을 사용하고 있으며, 오늘날까지 Drupal 7의 모든 공식 출시에는 기본적으로 jQuery 1.4.4가 포함되어 있습니다.
따라서 프로젝트에서 jQuery 1.6+를 사용할지 확실하지 않은 경우 모든 버전의 jQuery에서 사용할 수 있는 옵션 중 하나를 사용하십시오.
옵션 1:
jQuery('selector')[0].tagName.toLowerCase()
옵션 2
jQuery('selector')[0].nodeName.toLowerCase()
nodeName은 태그 이름을 대문자로 나타내고 localName은 소문자로 지정합니다.
$("yourelement")[0].localName
다음과 같은 정보를 얻을 수 있습니다.YORELEMENT 대신 yourrelement
jQuery를 사용하여 태그 이름 클릭
jQuery("YOUR_SELECTOR").click(function (e) {
console.log(jQuery(e.target).prop("tagName").toLowerCase())
})
언급URL : https://stackoverflow.com/questions/5347357/jquery-get-selected-element-tag-name
'programing' 카테고리의 다른 글
Chromecast 확장이 설치되지 않았거나 알 수 없는 기능을 사용하는 경우 Google Chromecast 전송자 오류 (0) | 2022.09.04 |
---|---|
팬더 칼럼을 날짜 시간으로 변환 (0) | 2022.09.04 |
정의되지 않은 함수 curl_init()를 호출합니다. (0) | 2022.09.04 |
변수는 내부 클래스 내에서 액세스됩니다.최종 선언 필요 (0) | 2022.09.04 |
도커의 MySql 데이터베이스 쿼리 (0) | 2022.09.04 |