选择文本事件

来源:互联网 发布:做淘宝推广工资怎么样 编辑:程序博客网 时间:2024/05/22 07:05
<!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>选择事件</title>

</head>
<body>
武汉

<div><span>湖北</span></div>

<div><span>中国</span></div>

</body>
<script type="text/javascript">
    
    var K = K || {};
    
    K.SelectEvent = function(e) {
        var q = K.GetSelectText();
        if ('' == q) return true;
        
        alert(q);
    };
    
    K.GetSelectText = function() {
      var q = '';
      try {
        if (window.getSelection) {
          q = window.getSelection();
        } else if (document.getSelection) {
          q = document.getSelection();
        } else if (document.selection) {
          q = document.selection.createRange().text;
        } else {
          q = '';
        }
        q = q.toString();
        q = q.replace(/((^/s*)|([/n/t/r]+)|(/s*$))/g, '');
        if (q.replace(/[^/x00-/xff]/g, "**").length > 15) {
          return '';
          q = q.substr(0, 15);
        }
      } catch(e) {}
      return q;
    };
    
    if (document.addEventListener) {
        document.addEventListener("mouseup", K.SelectEvent, true);
    } else if (document.attachEvent) {
        document.attachEvent("onmouseup", K.SelectEvent);
    } else {}
</script>
</html>

原创粉丝点击