如何判断一个元素是否为“br”? 我尝试使用 .attr("type") 和 .type() 获取它,但始终返回 undefined! 对于 input 或 button 元素有效,但对于 br 元素无效。
如何判断一个元素是否为“br”? 我尝试使用 .attr("type") 和 .type() 获取它,但始终返回 undefined! 对于 input 或 button 元素有效,但对于 br 元素无效。
.tagName
或.nodeName
属性,然后将其值与"BR"
进行比较。你可以尝试使用$(element).is('br')
,例如:
<div class='tested' id='first'>
<br class='tested' id='second' />
</div>
...
$('.tested').each(function() {
var id = this.id;
if ($(this).is('br')) {
console.log(id + ' is <br>');
}
else {
console.log(id + ' is not <br>');
}
});
// first is not <br>
// second is <br>
console.log($(this).parent().children().is('br'));
- mrmoor通过 element.tagName
可以获取标签的名称。
if (element.tagName=="br")
.attr("type")
在处理输入框或按钮时并不是以同样的方式工作:它并不能告诉你元素是<input>
还是<button>
,而是告诉你这个<input>
或<button>
的类型。 - nnnnnn