javascript中如何获得选中文字所在的节点
在:
<div>12345677aaaaa888888</div>
中,我在页面里面选中aaaaa,如果获得DIV这个节点?

解决方案 »

  1.   

    jquery的选择器是可以,那么原始的js当然也可以,不过还是用jquery方便
      

  2.   

    $("div:contains('aaaaa')"),直接给你个例子
      

  3.   


    <!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
           alert(direct.tagName + ': ' + direct.innerHTML);          
        }
      //-->
      </script>
     </head> <body>
     <div>12345677aaaaa888888 </div>
     <input type="button" value="ShowDirection" onclick="ShowDirection()" />
     </body>
    </html>