Jam*_*mes 59 html javascript css jquery
我如何检查是否$(this)是div,ul或blockquote?
例如:
if ($(this) is a div) {
alert('its a div!');
} else {
alert('its not a div! some other stuff');
}
Run Code Online (Sandbox Code Playgroud)
Bjo*_*orn 92
像这样的东西:
if(this.tagName == 'DIV'){
alert("It's a div!");
} else {
alert("It's not a div! [some other stuff]");
}
Run Code Online (Sandbox Code Playgroud)
MBO*_*MBO 42
没有jQuery的解决方案已经发布,所以我将使用jQuery发布解决方案
$(this).is("div,ul,blockquote")
Run Code Online (Sandbox Code Playgroud)
Mic*_*Mic 20
没有jQuery,你可以说 this.tagName === 'DIV'
请记住,'N' tagName是大写的.
或者,使用更多标签:
/DIV|UL|BLOCKQUOTE/.test(this.tagName)
小智 15
检查此元素是否为 DIV
if (this instanceof HTMLDivElement) {
alert('this is a div');
}
Run Code Online (Sandbox Code Playgroud)
HTMLUListElementUL相同,
HTMLQuoteElementblockquote
老问题,但由于没有一个答案提到这一点,一个没有 jquery 的现代替代方案可能只是使用 CSS 选择器和 Element.matches()
element.matches('div, ul, blockquote');
if(this.tagName.toLowerCase() == "div"){
//it's a div
} else {
//it's not a div
}
Run Code Online (Sandbox Code Playgroud)
编辑:当我写的时候,给出了很多答案,抱歉 doublure
| 归档时间: |
|
| 查看次数: |
68977 次 |
| 最近记录: |