javascript DOM2寻找选择文字路径
2009-09-11 00:31 BlueDream 阅读(409) 评论(0) 收藏 举报今天在CSDN遇到个帖子,需要得到选中元素的所属DOM结点层次.代码主要涉及的DOM2的几个点.
看下代码就知道了:
看下代码就知道了:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <script type="text/javascript"> function ShowDirection() { var tbl = []; var direct = (document.selection && document.selection.createRange) ? document.selection.createRange().parentElement() // IE : window.getSelection().focusNode.parentNode; // FF do { tbl.push(direct.tagName); } while((direct = direct.parentNode) && (direct !== document.documentElement)); alert(tbl.reverse().join('---->')) } </script> </head> <body> <div>123 <p>456 <span>789 <b>bbbbb </b> </span> </p> <input type="button" value="ShowDirection" onclick="ShowDirection()" /> </body> </html>