js自定义方法–addClass
function addclass(elm,newclass){
var classes = elm.className.split(' ');
classes.push(newclass);
elm.className = classes.join(' ');
}
split() 方法用于把一个字符串分割成字符串数组。
join() 方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。可指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。提示:要想数组的开头添加一个或多个元素,请使用 unshift() 方法。
a.x = a = { }, 深入理解赋值表达式
var o = {x : 1};
var a = o;
a.x = a = {name:100};
console.log(a.x); // undefined
console.log(o.x); // {name:100}
// a.x = a = {name:100};
// 等价于 a.x = (a = {name:100});
// 首先计算a.x的引用,然后计算(a = {name:100})的返回值
if 语句的简写
var condition = true, numb = 0;
if(condition) {
alert('rain-man')
}
if(condition) {
numb = 1 + 2;
}
等同于
var condition = true, numb = 0;
condition && alert('rain-man');
condition && (numb = 1 + 2);
&& 和 || 的计算取值
(true && 222); // 222
!!(true && 222); // true
(false && 222 ); // false
(false || 222); // 222
!!(false || 222); // true
!!variable 会返回和原值相等的boolean值
Object的构造
function Object() { [native code] }
Object.prototype = {
constructor: function Object() { [native code] },
hasOwnProperty: function hasOwnProperty() { [native code] },
isPrototypeOf: function isPrototypeOf() { [native code] },
propertyIsEnumerable: function propertyIsEnumerable() { [native code] },
toLocaleString: function toLocaleString() { [native code] },
toString: function toString() { [native code] },
valueOf: function valueOf() { [native code] }
};
Object.prototype.constructor === Object; // true
prototype中的一些细节
var A = function(){
this.name = 'rain-man';
};
A.prototype = {
name : 'cnblogs'
};
var o = new A();
console.log(o.name); // 'rain-man'
var B = function(){};
B.prototype = {
name : 'obj-B'
};
var o = new B();
o.name = 'obj-c';
delete o.name;
console.log(o.name); //'obj-B',暴漏原型链
创建对象,并保持原型链
var O = function(obj) {
function T() {}
T.prototype = obj;
return new T();
};
var obj = {name: 'obj', age: 0 },
obj1 = O(obj),
obj2 = O(obj1);
// 更改原型链的一处,所有原型链都会更改
obj.name = 'superclass';
console.log(obj1.name); // 'superclass'
console.log(obj2.name); // 'superclass'
// 每一层可单独处理
obj1.name = 100;
console.log(obj1.name); //100
delete obj1.name; //暴漏原型链
console.log(obj1.name); // 'superclass'
处理兼容性,获得事件对象
if(window.event) oEvent = window.event; 或者 oSrc = oEvent.srcElement?oEvent.srcElement:oEvent.target;
function downTime(){
var timeout = window.setTimeout(downTime, 100);
var endTime=new Date("12-21-2011 0:0:0");//改成你的计时日期
var today=new Date();
var timeold=(endTime.getTime()-today.getTime());
if(timeold <= 100){
window.clearTimeout(timeout);
document.getElementById("container").innerHTML = "原来没事啊,虚惊一场!";
return;
}
var sectimeold=timeold/1000;
var secondsold=Math.floor(sectimeold);
var msPerDay=24*60*60*1000;
var e_daysold=timeold/msPerDay;
var daysold=Math.floor(e_daysold);
var e_hrsold=(e_daysold-daysold)*24;
var hrsold=Math.floor(e_hrsold);
var e_minsold=(e_hrsold-hrsold)*60;
var minsold=Math.floor((e_hrsold-hrsold)*60);
var e_seconds=(e_minsold-minsold)*60;
var seconds=Math.floor((e_minsold-minsold)*60);
var decimals = Math.floor((e_seconds-seconds)*10);
document.getElementById("downtime").innerHTML=daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"."+decimals+"秒" ;
}
downTime();
图片等比例缩放
function newDrawImage(ImgD){
var image=new Image();
var iwidth = 300; //定义允许图片宽度
var iheight = 300; //定义允许图片高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width>=image.height){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=image.height*(iwidth/image.width);
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=image.width*(iheight/image.height);
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
img onload=""newDrawImage(this)"" src=""http://pic8.nipic.com/20100809/1660047_025137350646_2.jpg""
JS弹出窗口
function read(url){
var D=250
var F=540
if(window.screen.height){
H=(window.screen.height-D)/2
}
if(window.screen.width){
B=(window.screen.width-F)/2
}
var newswin=window.open
(url,"newswin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,re
sizable=no,top="+H+",left="+B+",width="+F+",height="+D+"");
newswin.focus();
}
a href=""http://www.baidu.com"" onclick=""read(this.href);return"
js 弹出窗口居中
/*弹出窗口居中*/
function openwindow(url,name,iWidth,iHeight)
{
var url; //转向网页的地址;
var name; //网页名称,可为空;
var iWidth; //弹出窗口的宽度;
var iHeight; //弹出窗口的高度;
//获得窗口的垂直位置
var iTop = (window.screen.availHeight-30-iHeight)/2;
//获得窗口的水平位置
var iLeft = (window.screen.availWidth-10-iWidth)/2;
window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');
}
a href=""
javascript:void(0)"" onclick=""openwindow('http://www.baidu.com','baidu',400,400)""
转载请注明:javascript小记 - 编程知识库