admin管理员组

文章数量:1395198

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 
}

本文标签: javascriptUsing tagName correctlyStack Overflow