您的位置:首页 > Web前端 > JavaScript

javascript DOM2寻找选择文字路径

2009-09-11 00:31 155 查看
今天在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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: