最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I see if an element is a "br" - Stack Overflow

programmeradmin1浏览0评论

How can I see if an element is a "br"? I have tried to get it via .attr("type") and .type() but it always returns undefined! It would work on a input or button but not on a br element.

How can I see if an element is a "br"? I have tried to get it via .attr("type") and .type() but it always returns undefined! It would work on a input or button but not on a br element.

Share Improve this question asked Aug 24, 2012 at 6:17 mrmoormrmoor 3011 gold badge6 silver badges11 bronze badges 2
  • if (elem.localName == 'br') {...} ? – Ram Commented Aug 24, 2012 at 6:21
  • .attr("type") doesn't work on an input or button in the same sense: it doesn't tell you that the element is an <input> or <button>, it tells you the type of the <input> or <button>. – nnnnnn Commented Aug 24, 2012 at 6:23
Add a comment  | 

4 Answers 4

Reset to default 9

Use the .tagName or .nodeName property. Then compare the value to "BR".

How about just $(element).is('br')? For example:

<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>​

by element.tagName you can get the name of the tag

if (element.tagName=="br")
<div id="test">
    <br>
</div>

--

var tag = $("#test").children().first().prop('tagName'); //returns BR

FIDDLE

var isTag = $("#test").children().first().is('br');  //returns true/false

FIDDLE

发布评论

评论列表(0)

  1. 暂无评论