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

javascript - Using .tagName correctly - Stack Overflow

programmeradmin0浏览0评论
if (((document.activeElement).parentNode).tagName == "div") { 
   //do amazing things 
}

For some reason a specific function of mine isn't working, and I think it has to do with the line above. Is the line above syntactically correct, or is wrong? If the line above is correct, I'll either delete the question (because then the question is useless) or add additional information of the rest of the function.

Thanks

if (((document.activeElement).parentNode).tagName == "div") { 
   //do amazing things 
}

For some reason a specific function of mine isn't working, and I think it has to do with the line above. Is the line above syntactically correct, or is wrong? If the line above is correct, I'll either delete the question (because then the question is useless) or add additional information of the rest of the function.

Thanks

Share Improve this question asked Aug 15, 2012 at 22:19 sir_thursdaysir_thursday 5,41913 gold badges68 silver badges121 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

The tagName gives the tag in upper-case so it would give DIV

See https://developer.mozilla/en-US/docs/DOM/element.tagName

You can use .toLowerCase() == 'div'(as suggested in the ments) to eliminate the case issue.

You don't need all the parens. To be safe, use tagName.toLowerCase().

if (document.activeElement.parentNode.tagName.toLowerCase() == "div") { 
   //do amazing things 
}

There may be some browsers that sometimes do not give the same case as others. This makes sure you always pare the same case.

In the context of HTML, the value in the tagName field is always uppercase. However, if you run this script in XML/XHTML context, it will return the name of the tag exactly as provided (and not necessarily uppercase).

Try calling document.activeElement.parentNode.tagName.toLowerCase() to make it always lowercase.

Try this

if (((document.activeElement).parentNode).tagName === "DIV") { 
   //do amazing things 
}
发布评论

评论列表(0)

  1. 暂无评论