function _getScreen(){
var s = {};
if(window.innerWidth){
s.width = window.innerWidth;
s.height = window.innerHeight;
}else {//IE
s.width = document.documentElement.clientWidth;
s.height = document.documentElement.clientHeight;
}
return s;
}
alert(_getScreen().width+' , '+_getScreen().height)
提示框tip div:
function _makeTip(_id){
$("#"+_id+"").hover(function(e){
e = e||window.event;
var div = "
"+this.title+"
";
$("body").append(div);
$("#_tip_div_id").css({
border:'1px solid black',
position:'absolute',
left:e.clientX+10+"px",
top:e.clientY+10+"px"
});
this.mytitle=this.title;
this.title='';
},function(e){
e = e||window.event;
this.title=this.mytitle;
this.mytitle='';
$("#_tip_div_id").remove();
}).mousemove(function(e){
e = e||window.event;
$("#_tip_div_id").css({
left:e.clientX+10+"px",
top:e.clientY+10+"px"
});
});
}
例如html:
This is my HTML page. This is my HTML page.
js:
$(function(){
_makeTip("_link");
});
元素可拖拽:
function _getScreen(){
var s = {};
if(window.innerWidth){
s.width = window.innerWidth;
s.height = window.innerHeight;
}else {//IE
s.width = document.documentElement.clientWidth;
s.height = document.documentElement.clientHeight;
}
return s;
}
function _dragable(ele){
ele.onmousedown=function(e){
e = e || window.event;
//首先阻止事件传播导致浏览器的默认行为
if(e.preventDefault){//非IE
e.preventDefault();
}else{//IE
e.returnValue = false;
}
var _this = ele;
var old_mouse_x = e.clientX;//鼠标最初的X坐标
var old_mouse_y = e.clientY;//鼠标最初的Y坐标
var old_ele_x = _this.offsetLeft;//元素最初的X坐标
var old_ele_y = _this.offsetTop;//元素最初的Y坐标
if(_this.setCapture){
_this.setCapture();
}
document.onmousemove=function(e){
e = e || window.event;
var new_mouse_x = e.clientX;//鼠标当前X坐标
var new_mouse_y = e.clientY;//鼠标当前Y坐标
var new_ele_x = (old_ele_x + new_mouse_x - old_mouse_x);//元素当前X坐标
var new_ele_y = (old_ele_y + new_mouse_y - old_mouse_y);//元素当前Y坐标
if(new_ele_x < 0){new_ele_x = 0;}
if(new_ele_y < 0){new_ele_y = 0;}
if(new_ele_x > (_getScreen().width-_this.offsetWidth)){new_ele_x = (_getScreen()-_this.offsetWidth);}
if(new_ele_y > (_getScreen().height-_this.offsetHeight)){new_ele_y = (_getScreen().height-_this.offsetHeight);}
_this.style.left = new_ele_x+'px';
_this.style.top = new_ele_y+'px';
}
document.onmouseup=function(e){
e = e || window.event;
this.onmousemove = null;
this.onmouseup = null;
if(_this.releaseCapture){
_this.releaseCapture();
}
}
}
}
例如:html:
function _isArray(val) {
if (!val) {
return false;
}
return Object.prototype.toString.call(val) === '[object Array]';
}
function _isFunction(val) {
if (!val) {
return false;
}
return Object.prototype.toString.call(val) === '[object Function]';
}
设置元素透明度:
IE:
filter:alpha(Opacity=40)
非IE:
opacity:0.4
遍历方法:
function _each(obj, fn) {
if (_isArray(obj)) {
for (var i = 0, len = obj.length; i < len; i++) {
if (fn.call(obj[i], i, obj[i]) === false) {
break;
}
}
} else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (fn.call(obj[key], key, obj[key]) === false) {
break;
}
}
}
}
}
trim 方法:
function _trim(str) {
return str.replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, '');
}
转为16进制(toHex)方法:
function _toHex(val) {
function hex(d) {
var s = parseInt(d, 10).toString(16).toUpperCase();
return s.length > 1 ? s : '0' + s;
}
return val.replace(/rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/ig,
function($0, $1, $2, $3) {
return '#' + hex($1) + hex($2) + hex($3);
}
);
}
获取css样式:
function _css(ele,attr){
if(typeof window.getComputedStyle != 'undefined'){//W3C
return window.getComputedStyle(ele,null)[attr];
}else if(typeof ele.currentStyle != 'undefined'){//IE
return ele.currentStyle[attr];
}
}
var ele = document.getElementById('_id');
_css(ele,'width')
往link文件中添加CSS样式:
function addRule(num,selectText,cssText,position){
var sheet = document.styleSheets[num];//获取第num个link文件
if(typeof sheet.insertRule != 'undefined'){//W3C
sheet.insertRule(selectText+'{'+cssText+'}',position);
}else if(typeof sheet.addRule != 'undefined'){//IE
sheet.addRule(selectText,cssText,position);
}
}
var ele = document.getElementById('_id');
addRule(0,'body','background:red',0);//在第一个link文件中的第一个位置添加如下的css: body{background:red}
绑定/解绑 事件:
function _bindEvent(el, type, fn) {
if (el.addEventListener){
el.addEventListener(type, fn, false);
} else if (el.attachEvent){
el.attachEvent('on' + type, fn);
}
}
function _unbindEvent(el, type, fn) {
if (el.removeEventListener){
el.removeEventListener(type, fn, false);
} else if (el.detachEvent){
el.detachEvent('on' + type, fn);
}
}
阻止事件传播:
function preventDefault () {
var ev = this.event;
if (ev.preventDefault) {
ev.preventDefault();
}
ev.returnValue = false;
}
function stopPropagation() {
var ev = this.event;
if (ev.stopPropagation) {
ev.stopPropagation();
}
ev.cancelBubble = true;
}
function stop () {
this.preventDefault();
this.stopPropagation();
}
利用JavaScript进行对象排序,根据用户的年龄排序展示
<script>
var bob={
name;bob,
age:30
}
var peter={
name;peter,
age:30
}
var amy={
name;amy,
age:24
}
var mike={
name;mike,
age:29
}
var john={
FLP
One famous theory in distributed computing, known as FLP after the authors Fischer, Lynch, and Patterson, proved that in a distributed system with asynchronous communication and process crashes,
每一行命令都是用分号(;)作为结束
对于MySQL,第一件你必须牢记的是它的每一行命令都是用分号(;)作为结束的,但当一行MySQL被插入在PHP代码中时,最好把后面的分号省略掉,例如:
mysql_query("INSERT INTO tablename(first_name,last_name)VALUES('$first_name',$last_name')");
题目链接:zoj 3820 Building Fire Stations
题目大意:给定一棵树,选取两个建立加油站,问说所有点距离加油站距离的最大值的最小值是多少,并且任意输出一种建立加油站的方式。
解题思路:二分距离判断,判断函数的复杂度是o(n),这样的复杂度应该是o(nlogn),即使常数系数偏大,但是居然跑了4.5s,也是醉了。 判断函数里面做了3次bfs,但是每次bfs节点最多