window.getSelection选中的文字,怎么知道是在什么标签中呢?<div>123
    <p>456
         <span>789
                <b>bbbbb</b>
         </span>
    </p>
</div>比如我当前选择的内容是12,如何告诉我是div标签;
再比如我选择了89bbb,如何告诉我:div->p->span->b
谢谢。

解决方案 »

  1.   

    document.selection.createRange().parentNode;//找到上级节点 试试 我猜的
    看lz名字,C++还真是个好东西,我也觉得 干吗来整js 好好修炼c++,
    我都想去学c++了 js画的在好也是在页面上话 与系统交互还是c++啊 
    纯属于个人yy 楼主别介意
      

  2.   


    <!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() 
    : window.getSelection().focusNode.parentNode();
    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>
      

  3.   

    晕 不好意思 FF下 多打了个括号...<!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>
      

  4.   

    这个发贴2小时以后我再网上找到答案了,我居然都不知道这个focusNode的存在,害我饶了山路十八弯,谢谢你们大家啊~