一些CSS/JS小技巧

时间:2021-11-22 09:37:08

CSS部分

1、文本框不可点击

.inputDisabled{ background-color: #eee;cursor: not-allowed;}

2、禁止复制粘贴

onpaste="return false" oncontextmenu="return false" oncopy="return false" oncut="return false"

JS部分

1、实现复选框部分选中状态

 <input id="chk1" type="checkbox" />
<script>
document.getElementById('chk1').indeterminate = true;
</script>

2、浅拷贝  参考:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html

 function extendCopy(p) {
var c = {};
for (var i in p) {      
c[i] = p[i];    
}
c.uber = p;
return c;  
}

3、深拷贝  参考:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html

function deepCopy(p, c) {    
var c = c || {};    
for (var i in p) {      
if (typeof p[i] === 'object') {        
c[i] = (p[i].constructor === Array) ? [] : {};        
deepCopy(p[i], c[i]);      
} else {         
c[i] = p[i];      
}    
}    
return c;  
}

4、生成lowerValue(包括)到 upperValue(包括)的随机整数。

 function selectFrom(lowerValue, upperValue) {
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}

5、确定属性是原型中的属性

 function hasPrototypeProperty(object, name) {
return !object.hasOwnProperty(name) && (name in object);
}

6、跨浏览器取得窗口左边和上边的位置

 var leftPos = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX;
var topPos = (typeof window.screenTop == "number") ? window.screenTop : window.screenY;

7、获取地址栏参数

 /*
获取页面url参数
调用方法:
alert(GetQueryString("参数名1"));
alert(GetQueryString("参数名2"));
*/
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
/*
获取页面url参数
调用方法:
Request = GetRequest();
var 参数1,参数2,参数3,参数N;
参数1 = Request['参数1'];
参数2 = Request['参数2']; */
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}

8、计算元素在页面的偏移量

 //左偏移量
function getElementLeft(element) {
var actualLeft = element.offsetLeft;
var current = element.offsetParent;
while (current !== null) {
actualLeft += current.offsetLeft;
current = current.offsetParent;
}
return actualLeft;
}
//上偏移量
function getElementTop(element) {
var actualTop = element.offsetTop;
var current = element.offsetParent;
while (current !== null) {
actualTop += current.offsetTop;
current = current.offsetParent;
}
return actualTop;
}

9、判断浏览器类型

 function getBrowserType() {
var ua = navigator.userAgent.toLowerCase(), browserType = "";
if (window.ActiveXObject) {
//Sys.ie = ua.match(/msie ([\d.]+)/)[1];
Sys.ie = ua;
browserType = "IE";
}
else if (document.getBoxObjectFor) {
//Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1];
Sys.firefox = ua;
browserType = "firefox";
}
else if (window.MessageEvent && !document.getBoxObjectFor) {
//Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1];
Sys.chrome = ua;
browserType = "chrome";
}
else if (window.opera) {
//Sys.opera = ua.match(/opera.([\d.]+)/)[1];
Sys.opera = ua;
browserType = "opera";
}
else if (window.openDatabase) {
//Sys.safari = ua.match(/version\/([\d.]+)/)[1];
Sys.safari = ua;
browserType = "safari";
}
return browserType;
}