JS代码示例
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(); }};6. 创建事件<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();}}8.查找页面中的字符?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); }}??9.字符替换??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 standardized. // getSelection() returns a Selection object, which we do not document. return window.getSelection().toString(); } else if (document.getSelection) { // This is an older, simpler technique that returns a string return document.getSelection(); } else if (document.selection) { // This is the IE-specific technique. // We do not document the IE selection property or TextRange objects. return document.selection.createRange().text; }}11.鼠标滚轮事件?
function handleMouseWheel(e){ if (!e) e = window.event; if (e.wheelDelta <= 0 || e.detail > 0) {document.getElementById('t').innerHTML='滚轮向下滚?'} else {document.getElementById('t').innerHTML='滚轮向上滚?'}}if(document.attachEvent||/(?:ie|webkit)/i.test(navigator.appVersion)){ document.onmousewheel = handleMouseWheel;}else{ window.addEventListener("DOMMouseScroll", handleMouseWheel, false);}?