原地址:http://hi.baidu.com/thinsoft/blog/item/9d159a1bdb6939f9ae513319.html
应该采用:
document.getElementById(”apple”) 以ID来访问对象,且一个ID在页面中必须是唯一的
document.getElementsByTagName(”div”)[0] 以标签名来访问对象
1.setAttribute(string name, string value):增加一个指定名称和值的新属性,或者把一个现有的属性设定为指定的值。
设置对象的属性则应该采用:
document.getElementById(”apple”).setAttribute(”width”,”100″)
document.getElementsByTagName(”div”)[0].setAttribute(”width”,”100″)
访问对象的属性则采用:
document.getElementById(”apple”).getAttribute(”width”)
document.getElementsByTagName(”div”)[0].getAttribute(”width”)
我们经常需要在JavaScript中给Element动态添加各种属性,这可以通过使用setAttribute()来实现,这就涉及到了浏览器的兼容性问题。
var bar = document.getElementById(”foo”);
bar.setAttribute(”onclick”, “javascript:alert(’This is a test!’);”);
这里利用setAttribute指定e的onclick属性,简单,很好理解。但是IE不支持,IE并不是不支持setAttribute这个函数,
而是不支持用setAttribute设置某些属性,例如对象属性、集合属性、事件属性,也就是说用setAttribute设置style和onclick这些属性
在IE中是行不通的。为达到兼容各种浏览器的效果,可以用点符号法来设置Element的对象属性、集合属性和事件属性。
document.getElementById(”foo”).className = “fruit”;
document.getElementById(”foo”).style.cssText = “color: #00f;”;
document.getElementById(”foo”).style.color = “#00f”;
document.getElementById(”foo”).onclick= function () { alert(”This is a test!”); }
2、关于class和className
class属性在W3C DOM中扮演着很重要的角色,但由于浏览器差异性仍然存在。使用setAttribute(”class”, vName)语句动态设置
Element的class属性在firefox中是行的通的,在IE中却不行。因为使用IE内核的浏览器不认识”class”,要改用”className”;
同样,firefox 也不认识”className”。所以常用的方法是二者兼备:
element.setAttribute(”class”, vName);
element.setAttribute(”className”, vName); //for IE
关于IE下TABLE无法插入新行的问题
IE下TABLE无论是用innerHTML还是appendChild插入<tr>都没有效果,而其他浏览器却显示正常。解决他的方法是,将<tr>加到TABLE的<tbody>元素中,如下面所示:
var row = document.createElement(”tr”);
var cell = document.createElement(”td”);
var cell_text = document.createTextNode(”香蕉不吃苹果”);
cell.appendChild(cell_text);
row.appendChild(cell);
document.getElementsByTagName(”tbody”)[0].appendChild(row);
window.event
IE:有window.event对象
FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event)
鼠标当前坐标
IE:event.x和event.y。
FF:event.pageX和event.pageY。
通用:两者都有event.clientX和event.clientY属性。
鼠标当前坐标(加上滚动条滚过的距离)
IE:event.offsetX和event.offsetY。
FF:event.layerX和event.layerY。
标签的x和y的坐标位置:style.posLeft 和 style.posTop
IE:有。
FF:没有。
通用:object.offsetLeft 和 object.offsetTop。
窗体的高度和宽度
IE:document.body.offsetWidth和document.body.offsetHeight。注意:此时页面一定要有body标签。
FF:window.innerWidth和window.innerHegiht,以及document.documentElement.clientWidth和document.documentElement.clientHeight。
通用:document.body.clientWidth和document.body.clientHeight。
添加事件
IE:element.attachEvent(”onclick”, func);。
FF:element.addEventListener(”click”, func, true)。
通 用:element.onclick=func。虽然都可以使用onclick事件,但是onclick和上面两种方法的效果是不一样的, onclick只有执行一个过程,而attachEvent和addEventListener执行的是一个过程列表,也就是多个过程。例如: element.attachEvent(”onclick”, func1);element.attachEvent(”onclick”, func2)这样func1和func2都会被执行。
标签的自定义属性
IE:如果给标签div1定义了一个属性value,可以div1.value和div1["value"]取得该值。
FF:不能用div1.value和div1["value"]取。
通用:div1.getAttribute(”value”)。
父节点、子节点和删除节点
IE:parentElement、parement.children,element.romoveNode(true)。
FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。
CSS:透明
IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
FF:opacity:0.6。
设置CSS 的STYLE
document.getElementById(’look’).style.cssText=”display:none;”;//通用
document.getElementById(’look’).setAttribute(”style”,”display:none;”);//firefox
JS文件代码(将下面代码另存为JS文件):
var delta=0.15
var collection;
function floaters() {
this.items = [];
this.addItem = function(id,x,y,content)
{
document.write(’<DIV id=’+id+’
style=”Z-INDEX: 10; POSITION: absolute; width:80px;
height:60px;left:’+(typeof(x)==’string’?eval(x):x)+’;top:’+(typeof(y)==’string’?eval(y):y)+’”>’+content+’</DIV>’);
var newItem = {};
newItem.object =
document.getElementById(id);
newItem.x = x;
newItem.y = y;
this.items[this.items.length] = newItem;
}
this.play = function()
{
collection = this.items
setInterval(’play()’,10);
}
}
function play()
{
for(var i=0;i<collection.length;i++)
{
var followObj = collection[i].object;
var followObj_x = (typeof(collection[i].x)==’string’?eval(collection[i].x):collection[i].x);
var followObj_y = (typeof(collection[i].y)==’string’?eval(collection[i].y):collection[i].y);
if(followObj.offsetLeft!=(document.body.scrollLeft+followObj_x)) {
var dx=(document.body.scrollLeft+followObj_x-followObj.offsetLeft)*delta;
dx=(dx>0?1:-1)*Math.ceil(Math.abs(dx));
followObj.style.left=followObj.offsetLeft+dx;
}
if(followObj.offsetTop!=(document.body.scrollTop+followObj_y))
{
var
dy=(document.body.scrollTop+followObj_y-followObj.offsetTop)*delta;
dy=(dy>0?1:-1)*Math.ceil(Math.abs(dy));
followObj.style.top=followObj.offsetTop+dy;
}
followObj.style.display = ”;
}
}
var theFloaters = new floaters();
theFloaters.addItem(’followDiv1′,’document.body.clientWidth-100′,230,’<a
href=http://www.bbsgodo.com/ target=_blank><img src=你的图片地址
长宽度等参数设置></a><br><br><a
href=http://www.bbsgodo.com/ target=_blank><img src=你的图片地址
长宽度等参数设置></a>’);
theFloaters.addItem(’followDiv2′,12,230,’<a
href=http://www.bbsgodo.com/ target=_blank><img src=你的图片地址
长宽度等参数设置></a><br><br><a
href=http://www.bbsgodo.com/ target=_blank><img src=你的图片地址
长宽度等参数设置></a>’);
theFloaters.play();
依上面链接类推,你可以做N个广告链接!!
WinXP sp2/win2003 sp1/3721 拦截不了的弹窗代码<Script Language=”JavaScript”>
var paypopupURL = ”http://www.bbsgodo.com“; var usingActiveX = true; function blockError(){return true;} window.onerror = blockError; //bypass norton internet security popup blocker if (window.SymRealWinOpen){window.open = SymRealWinOpen;} if (window.NS_ActualOpen) {window.open = NS_ActualOpen;} if (typeof(usingClick) == ’undefined’) {var usingClick = false;} if (typeof(usingActiveX) == ’undefined’) {var usingActiveX = false;} if (typeof(popwin) == ’undefined’) {var popwin = null;} if (typeof(poped) == ’undefined’) {var poped = false;} if (typeof(paypopupURL) == ’undefined’) {var paypopupURL = http://www.bbsgodo.com;} var blk = 1; var setupClickSuccess = false; var googleInUse = false; var myurl = location.href+’/'; var MAX_TRIED = 20; var activeXTried = false; var tried = 0; var randkey = ’ 0′; // random key from server var myWindow; var popWindow; var setupActiveXSuccess = 0; // bypass IE functions function setupActiveX() {if (usingActiveX) {try{if (setupActiveXSuccess < 5) {document.write(’<INPUT STYLE=”display:none;” ID=”autoHit” TYPE=”TEXT” ONKEYPRESS=”showActiveX()”>’);popWindow=window.createPopup();popWindow.document.body.innerHTML=’<DIV ID=”objectRemover”><OBJECT ID=”getParentDiv” STYLE=”position:absolute;top:0px;left:0px;” WIDTH=1 HEIGHT=1 DATA=”‘+myurl+’/paypopup.html” TYPE=”text/html”></OBJECT></DIV>’;document.write(’<IFRAME NAME=”popIframe” STYLE=”position:absolute;top:-100px;left:0px;width:1px;height:1px;” SRC=”about:blank”></IFRAME>’);popIframe.document.write(’<OBJECT ID=”getParentFrame” STYLE=”position:absolute;top:0px;left:0px;” WIDTH=1 HEIGHT=1 DATA=”‘+myurl+’/paypopup.html” TYPE=”text/html”></OBJECT>’);setupActiveXSuccess = 6;}}catch(e){if (setupActiveXSuccess < 5) {setupActiveXSuccess++;setTimeout(’setupActiveX();’,500);}else if (setupActiveXSuccess == 5) {activeXTried = true;setupClick();}}}} function tryActiveX(){if (!activeXTried && !poped) {if (setupActiveXSuccess == 6 && googleInUse && popWindow && popWindow.document.getElementById(’getParentDiv’) && popWindow.document.getElementById(’getParentDiv’).object && popWindow.document.getElementById(’getParentDiv’).object.parentWindow) {myWindow=popWindow.document.getElementById(’getParentDiv’).object.parentWindow;}else if (setupActiveXSuccess == 6 && !googleInUse && popIframe && popIframe.getParentFrame && popIframe.getParentFrame.object && popIframe.getParentFrame.object.parentWindow){myWindow=popIframe.getParentFrame.object.parentWindow;popIframe.location.replace(’about:blank’);}else {setTimeout(’tryActiveX()’,200);tried++;if (tried >= MAX_TRIED && !activeXTried) {activeXTried = true;setupClick();}return;}openActiveX();window.windowFired=true;self.focus();}} function openActiveX(){if (!activeXTried && !poped) {if (myWindow && window.windowFired){window.windowFired=false;document.getElementById(’autoHit’).fireEvent(”onkeypress”,(document.createEventObject().keyCode=escape(randkey).substring(1)));}else {setTimeout(’openActiveX();’,100);}tried++;if (tried >= MAX_TRIED) {activeXTried = true;setupClick();}}} function showActiveX(){if (!activeXTried && !poped) {if (googleInUse) {window.daChildObject=popWindow.document.getElementById(’objectRemover’).children(0);window.daChildObject=popWindow.document.getElementById(’objectRemover’).removeChild(window.daChildObject);}newWindow=myWindow.open(paypopupURL,’abcdefg’);if (newWindow) {newWindow.blur();self.focus();activeXTried = true;poped = true;}else {if (!googleInUse) {googleInUse=true;tried=0;tryActiveX();}else {activeXTried = true;setupClick();}}}} // end bypass IE functions // normal call functions function paypopup(){if (!poped) {if(!usingClick && !usingActiveX) {popwin = window.open(paypopupURL,’abcdefg’);if (popwin) {poped = true;}self.focus();}}if (!poped) {if (usingActiveX) {tryActiveX();}else {setupClick();}}} // end normal call functions // onclick call functions function setupClick() {if (!poped && !setupClickSuccess){if (window.Event) document.captureEvents(Event.CLICK);prePaypopOnclick = document.onclick;document.onclick = gopop;self.focus();setupClickSuccess=true;}} function gopop() {if (!poped) {popwin = window.open(paypopupURL,’abcdefg’);if (popwin) {poped = true;}self.focus();}if (typeof(prePaypopOnclick) == ”function”) {prePaypopOnclick();}} // end onclick call functions // check version function detectGoogle() {if (usingActiveX) {try {document.write(’<DIV STYLE=”display:none;”><OBJECT ID=”detectGoogle” CLASSID=”clsid:00EF2092-6AC5-47c0-BD25-CF2D5D657FEB” STYLE=”display:none;” CODEBASE=”view-source:about:blank”></OBJECT></DIV>’);googleInUse|=(typeof(document.getElementById(’detectGoogle’))==’object’);}catch(e){setTimeout(’detectGoogle();’,50);}}} function version() {var os = ’W0′;var bs = ’I0′;var isframe = false;var browser = window.navigator.userAgent;if (browser.indexOf(’Win’) != -1) {os = ’W1′;}if (browser.indexOf(”SV1″) != -1) {bs = ’I2′;}else if (browser.indexOf(”Opera”) != -1) {bs = ”I0″;}else if (browser.indexOf(”Firefox”) != -1) {bs = ”I0″;}else if (browser.indexOf(”Microsoft”) != -1 || browser.indexOf(”MSIE”) != -1) {bs = ’I1′;}if (top.location != this.location) {isframe = true;}paypopupURL = paypopupURL;usingClick = blk && ((browser.indexOf(”SV1″) != -1) || (browser.indexOf(”Opera”) != -1) || (browser.indexOf(”Firefox”) != -1));usingActiveX = blk && (browser.indexOf(”SV1″) != -1) && !(browser.indexOf(”Opera”) != -1) && ((browser.indexOf(”Microsoft”) != -1) || (browser.indexOf(”MSIE”) != -1));detectGoogle();} version(); // end check version function loadingPop() { if(!usingClick && !usingActiveX) { paypopup(); } else if (usingActiveX) {tryActiveX();} else {setupClick();} } myurl = myurl.substring(0, myurl.indexOf(’/',8)); if (myurl == ”) {myurl = ’.';} setupActiveX(); loadingPop(); self.focus(); </Script> |
自动选择最快的镜像服务器代码
速度测试中……
<script language=”JavaScript” type=”text/javascript”>
<!–
function will() {
if (event.srcElement.tagName==’A'||event.srcElement.tagName==’AD’) {
window.focus();
oUrl.style.display = “”;
yAd.style.display = “none”;
}
}
//–>
</script>
<base target=”_blank” onclick=”JavaScript:will()”>
<div id=”yAd”>
<div align=”center”>支持本站的发展,点下面的链接!<br/><br/><br/></div>
<div align=”center”>
在这加上广告代码</div>
</div>
<div id=”oUrl” style=”display:none;”>
<p align=”center”>点广告后显示地址</div>