日期:2014-05-16 浏览次数:20485 次
1.数组操作时的push?
var a = [],b = [],c = []; a.push([1,2]); a.push([3,4]); Array.prototype.push.apply(b,[1,2]); Array.prototype.push.apply(b,[3,4]); c.push(5,6); c.push(7,8); alert(a+'\n'+a.length+'\n'+a[0]); alert(b+'\n'+b.length+'\n'+b[0]); alert(c+'\n'+c.length+'\n'+c[0]);
?2.获得当前样式?
function getCurrentStyle(oElement, sProperty) {
if(oElement.currentStyle){
return oElement.currentStyle[sProperty];
}else if(window.getComputedStyle){
sProperty = sProperty.replace(/([A-Z])/g, "-$1").toLowerCase();
return window.getComputedStyle(oElement, null).getPropertyValue(sProperty);
}else{
return null;
}
}
?3. 移动数组中元素位置
Array.prototype.moveComponent=function (s,t){
if(s!=t) {
var temp=this[s];
this.splice(s,1);
this.splice(t,0,temp);
}
return this;
}
var a=new Array(0,1,2,3,4,5,6,7);
alert(a);
alert(a.moveComponent(2,6));
?4.校验密码级别
var checkPassWord = function (string) {
return string.replace(/^(?:(?=.{4})(?=.*([a-z])|.)(?=.*([A-Z])|.)(?=.*(\d)|.)(?=.*(\W)|.).*|.*)$/, "$1$2$3$4").length;
};
alert(checkPassWord("JS示例代码"));
?5.清除文件选择框中的内容?
<input type="file" id="file_choose" /><button onclick="clearf();">clear</button>
var clearf = function(){
var dx = document.getElementById('file_choose');
dx.value = '';
if(document.selection){
dx.select();
document.selection.clear();
}
};
<div id="d" onclick="alert(1)"> </div>
window.onload = function () {
var d = document.getElementById('d');
if (d.addEventListener) {
d.addEventListener('click', function () {
alert(2);
}, false);
var evt = document.createEvent('MouseEvents');
evt.initEvent('click', true, true);
d.dispatchEvent(evt);
} else {
d.attachEvent('onclick', function () {
alert(3);
});
d.click();
}
}
if(document.createRange){
var range = document.createRange();
}else
var range = document.body.createTextRange();
if(range.findText){
while(range.findText("字符")){
range.pasteHTML(range.text.fontcolor("#4499ee"));
range.collapse(true);
}
}else{
var s,n;
s = window.getSelection();
while(window.find("字符")){
var n = document.createElement("SPAN");
n.style.color="#ff0000"
s.getRangeAt(0).surroundContents(n);
}
}
??String.prototype.format=function()
{
if(arguments.length==0) return this;
for(var s=this, i=0; i<arguments.length; i++)
s=s.replace(new RegExp("\\{"+i+"\\}","g"), arguments[i]);
return s;
};?
10.获得页面中选中的字符串?
?function getSelectedText() {
if (window.getSelection) {
// This technique is the most likely to be standard