首页 > 代码库 > 鼠标经过, 选中文字

鼠标经过, 选中文字

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div id="a">aaa</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript">
     
      $("#a").hover(function(){
            this.focus();
        if(window.getSelection){
            var range=document.createRange();
            range.selectNodeContents(this);
            var selection = window.getSelection();
            selection.removeAllRanges();
            selection.addRange(range)            
            }
        else if(document.selection){
            //for ie
            var range=document.body.createTextRange()
            range.moveToElementText(this)
            range.select();
        }
      },function(){
      if(window.getSelection){
            var range=document.createRange();
            range.selectNodeContents(this);
            var selection = window.getSelection();
            selection.removeAllRanges();
             
            }
        else if(document.selection){
            //for ie
            var range=document.body.createTextRange()
            range.moveToElementText(none)
            
        }
      });
     
</script>
    
</body>
</html>

 

鼠标经过, 选中文字