文本框文字处理工具类js+代码高亮组件

CodeMirror是一个很好的高亮编辑js库

//////// 文本框文字处理 ////////
/*
input 对象
if selectionStart=selectionEnd is point
else 是string
*/
function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {   //firefox
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }else if (input.createTextRange) { //ie
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}
function setCaretToEnd (input) {
  setSelectionRange(input, input.value.length, input.value.length);
}
function setCaretToBegin (input) {
  setSelectionRange(input, 0, 0);
}
//自定义光标插入点位置
function setCaretToPos (input, pos) {
  setSelectionRange(input, pos, pos);
}
/*光标选中对象中的string
input 对象 eg:"Visit W3School...";
string 是匹配字符串  eg: "w3school"
*/
function selectString (input, string) {
    //RegExp(string, "i")  string 不区分大小写
  var match = new RegExp(string, "i").exec(input.value);//input.value 是否匹配有string
  if (match) {
    setSelectionRange (input, match.index, match.index + match[0].length);
  }
}
//替换string
function replaceSelection (input, replaceString) {
  if (input.setSelectionRange) {
    var selectionStart = input.selectionStart;
    var selectionEnd = input.selectionEnd;
    input.value = input.value.substring(0, selectionStart)
                  + replaceString
                  + input.value.substring(selectionEnd);
    if (selectionStart != selectionEnd) // has there been a selection
      setSelectionRange(input, selectionStart, selectionStart +replaceString.length);
    else // set caret
      setCaretToPos(input, selectionStart + replaceString.length);
  }
  else if (document.selection) {
    var range = document.selection.createRange();
    if (range.parentElement() == input) {
      var isCollapsed = range.text == '';
      range.text = replaceString;
      if (!isCollapsed)  { // there has been a selection
        //it appears range.select() should select the newly
        //inserted text but that fails with IE
        range.moveStart('character', -replaceString.length);
        range.select();
      }
    }
  }
}


/**
* 文本框光标定位
**/
function setCaretPosition(elemId, caretPos) {
    var elem = document.getElementById(elemId);
    if(elem != null&&caretPos!=0) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.setSelectionRange(caretPos, caretPos);
            }
            elem.focus();
        }
        elem.scrollTop = elem.scrollHeight; 
    }
}

弹出框div页面居中/遮罩

function _getposition(){
    var p = {};
    var scrollWidth, scrollHeight;
    if(window.innerHeight && window.scrollMaxY){
        scrollWidth = window.innerWidth + window.scrollMaxX;
        scrollHeight = window.innerHeight + window.scrollMaxY;
    }else if(document.body.scrollHeight>document.body.offsetHeight){
        scrollWidth = document.body.scrollWidth;
        scrollHeight = document.body.scrollHeight;
    }else{
        scrollWidth = document.body.offsetWidth;
        scrollHeight = document.body.offsetHeight;
    }
    if(self.innerHeight){
        if(document.documentElement.clientWidth){
            p.windowWidth = document.documentElement.clientWidth;
        }else{
            p.windowWidth = self.innerWidth;
        }
        p.windowHeight = self.innerHeight;
    }else if(document.documentElement && document.documentElement.clientHeight){
        p.windowWidth = document.documentElement.clientWidth;
        p.windowHeight = document.documentElement.clientHeight;
    }else if(document.body){
        p.windowWidth = document.body.clientWidth;
        p.windowHeight = document.body.clientHeight;
    }
    if(scrollWidth < p.windowWidth){
        p.width = scrollWidth;
    }else{
        p.width = p.windowWidth;
    }
    if(scrollHeight < p.windowHeight){
        p.height = scrollHeight;
    }else{
        p.height = p.windowHeight;
    }
    p.windowWidth = Math.max(p.windowWidth, scrollWidth);
    p.windowHeight = Math.max(p.windowHeight, scrollHeight);

    if(typeof(window.pageXOffset) == "number"){
        p.left = window.pageXOffset;
    }else if(document.documentElement && document.documentElement.scrollLeft){
        p.left = document.documentElement.scrollLeft;
    }else if(document.body){
        p.left = document.body.scrollLeft;
    }else if(window.scrollX){
        p.left = window.scrollX;
    }

    if(typeof(window.pageYOffset) == "number"){
        p.top = window.pageYOffset;
    }else if(document.documentElement && document.documentElement.scrollTop){
        p.top = document.documentElement.scrollTop;
    }else if(document.body){
        p.top = document.body.scrollTop;
    }else if(window.scrollY){
        p.top = window.scrollY;
    }
    return p;
}
var p = _getposition();
var left = p.left + ((p.width - $("#ddl_egg_msg_div").width()) / 2);
var top = p.top + ((p.height - $("#ddl_egg_msg_div").height()) / 2);						
$("#ddl_egg_msg_div").css({left:left,top:top});
$("#coverdiv").width(p.width).height(p.windowHeight).show();//整页遮罩

 div出现在页面的随机位置抽奖程序

var sHeight = jQuery(document.body).outerHeight(true) - 100;
var sWidth = jQuery(document.body).outerWidth(true) - 100;

var div_top  = GetRandomNum(0,sHeight);
var div_left = GetRandomNum(0,sWidth);

jQuery("#div").css("position","absolute");
jQuery("#div").css("z-index",100000);
jQuery("#div").css("top",div_top);
jQuery("#div").css("left",div_left);

function GetRandomNum(Min,Max)
{
    var Range = Max - Min;
    var Rand = Math.random();
    return(Min + Math.round(Rand * Range));
}

 抽奖金额的概率

<?php
$arr = array(20, 20, 20, 20, 20, 30, 30, 30, 30, 50);
$k = array_rand($arr);
$giftValue = $arr[$k];
//echo $giftValue;
?>

 

 

你可能感兴趣的:(IE,firefox)