codemirror是一个很好的高亮编辑js库
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
//////// 文本框文字处理 ////////
/*
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页面居中/遮罩
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
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出现在页面的随机位置,抽奖程序
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
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));
抽奖金额的概率
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
<?php
$arr = array(20, 20, 20, 20, 20, 30, 30, 30, 30, 50);
$k = array_rand($arr);
$giftvalue = $arr[$k];
//echo $giftvalue;
?>