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;
p.width = p.windowwidth;
if(scrollheight < p.windowheight){
p.height = scrollheight;
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;
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;
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;
?>