整理了一些有用的js方法

 一、清空选中

window.getSelection?window.getSelection().removeAllRange():document.selection.empty();

二、操纵样式

var oCss=document.styleSheet[0].cssRules || document.styleSheet[0].rules;

alert(oCss[0].style.background);

三、得到元素的样式

IE有obj.currentStyle.background.

DOM有document.defaultView.getComputedStyle(oDiv,null).background.

四、选中文本框的内容

IE有如下:

$('id').onfocus=function{

var cc=this.createTextRange(),r=cc.duplicate();

//cc.moveStart('character',0);//从第几个字符开始;

//cc.setEndPoint('EndToStart',0);

//cc.collapse(true);//合并,可用alert(cc.boundingWidth)来查看结果;

cc.select();

}

DOM有如下:

this.setSelectionRange(0,length);//length表示文本框的内容长度。

五、做拖动时,通用用清除选中内容的代码

window.getSelection?window.getSelection().removeAllRanges():document.selection.empty();

$('id').setCapture(false);//ie

document.releaseCapture();//DOM

六、判断节点之间的包含关系

document.defaultView?function(a,b){

return !!(a.compareDocumentPosition(b)&16);

}:function(a,b){

return a!=b&&a.contains(b);

}

七、caller、callee的用法

caller是方法的一个属性,返回当前调用该方法的方法,如:

function one(){two()};

function tow(){alert(two.caller)};

one();//结果弹出one的方法体.

callee属性是arguments的一个属性,返回方法的正文。

八、DOM下获取元素left、top的getBoundingClientRect();

如:

obj.getBoundingClientRect().left;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值