javascript - Using .tagName correctly - Stack Overflow

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

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 
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744760098a4592120.html

相关推荐

  • javascript - Using .tagName correctly - Stack Overflow

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

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信