text - Javascript : Get Parent Node Name - Stack Overflow

How should I get the Parent nodeName of a text while I'm on it with the cursor?<div id="ho

How should I get the Parent nodeName of a text while I'm on it with the cursor?

<div id="holder" contentEditable="true">
    Stackoverflow is the <b>coolest</b> Q&A website in the world.
</div>

And as a result we have:

  • Stackoverflow is the coolest Q&A website in the world.

So if the cursor is on the coolest I would like to get it's parent nodeName which is b

Please no libraries, just pure javascript.

How should I get the Parent nodeName of a text while I'm on it with the cursor?

<div id="holder" contentEditable="true">
    Stackoverflow is the <b>coolest</b> Q&A website in the world.
</div>

And as a result we have:

  • Stackoverflow is the coolest Q&A website in the world.

So if the cursor is on the coolest I would like to get it's parent nodeName which is b

Please no libraries, just pure javascript.

Share Improve this question edited Feb 3, 2011 at 21:52 Adam Halasz asked Feb 3, 2011 at 21:26 Adam HalaszAdam Halasz 58.4k67 gold badges153 silver badges216 bronze badges 1
  • Are you okay with a jQuery solution? – Matt Ball Commented Feb 3, 2011 at 21:33
Add a ment  | 

2 Answers 2

Reset to default 7
if (document.addEventListener) {
    document.getElementById('holder').addEventListener('mouseover', function (e) {
        somevar = e.target.nodeName;
    }, false);
} else {
    document.getElementById('holder').attachEvent('onmouseover', function (e) {
        somevar = e.srcElement.nodeName;
    });
}

EDIT: updated code and example in accordance with question edit and ments.

See example.

<div id="holder" contentEditable="true">
   Stackoverflow is the <b onclick="alert(this.tagName)">coolest</b> Q&A website in the world.
</div>

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

相关推荐

  • text - Javascript : Get Parent Node Name - Stack Overflow

    How should I get the Parent nodeName of a text while I'm on it with the cursor?<div id="ho

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信