常用跨浏览器javascript几例

本文介绍了几种跨浏览器的JavaScript编程技巧,包括向表格追加行、设置元素样式、创建单选按钮等,确保代码能在不同浏览器中正确运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据众浏览器的特点,从《Foundations of Ajax》中总结几则跨浏览器的javascript代码如下:

[b]1. 向表中追加行[/b]

为了写出通用的Javascript代码实现向表中追加行,必须将新增加的行append到tbody元素中(IE),而不能直接append到table元素。

<table id="myTable">
<tbody id="myTableBody"></tbody>
</table>


var cell=document.createElement("td").appendChild(document.createTextNode("foo"));
var row=document.createElement("tr").appendChild(cell);
document.getElementById("myTableBody").appendChild(row);

以上代码可实现包括IE在内的所有浏览器兼容。

[b]2. 通过javascript设置元素样式[/b]

除IE外可使用如下代码实现span内文字样式的改变:

var spanEle=document.getElementById("mySpan");
spanEle.setAttribute("style","font-weight:bold;color:red;");

但IE是个例外,必须使用点记法加cssText属性设置:

var spanEle=document.getElementById("mySpan");
spanEle.style.cssText="font-weight:bold;color:red;";

但这样又有人不高兴了,这个人叫Opera,所以要实现完全通用,就得双管齐下:

var spanEle=document.getElementById("mySpan");
spanEle.setAttribute("style","font-weight:bold;color:red;");
spanEle.style.cssText="font-weight:bold;color:red;";

哎,真是众口难调啊!

[b]3. 设置元素的class属性:[/b]

2中提到使用style属性设置元素的内联样式,其实设置元素的class属性同样存在一些特异性,这个异类就是IE。没有办法,IE仍然占有相当大的市场,我们还得顾着它。且看代码:

非IE中使用class作为属性名:

var ele=document.getElementById("myElement");
ele.setAttribute("class","styleClass");

IE中非要用className,“我就个性,怎么着吧?”:

var ele=document.getElementById("myElement");
ele.setAttribute("className","styleClass");

通用办法,双管齐下:

var ele=document.getElementById("myElement");
ele.setAttribute("class","styleClass");
ele.setAttribute("className","styleClass");


[b]4. 创建输入元素[/b]

这里强调的是创建子元素,设置子元素属性及将子元素追加到父元素的顺序,如不按这个顺序,某些浏览器可能会产生不同的效果。

var button=document.createElement("input");
button.setAttribute("type","button");
document.getElementById("myForm").appendChild(button);


5. 设置元素的事件处理程序

在非IE浏览器中,可以像设置元素属性一样设置元素的事件处理程序,如下所示:

var ele=document.getElementById("myElement");
ele.setAttribute("onclick","doFoo();");

但在IE中,必须使用点记法加匿名函数的方式:

var ele=document.getElementById("myElement");
ele.onclick=function(){doFoo();};

还好大家都很宽容,都支持这种点记法加匿名函数的方式,所以程序员只需要用这种方式就可以了。

[b]5. 创建单选按钮[/b]

非IE中:

var radioButton=document.createElement("input");
radioButton.setAttribute("type","radio");
radioButton.setAttribute("name","radioButton");
radioButton.setAttribute("value","checked");

但这样优雅的代码在IE中却不能完全正常工作,体现在生成的按钮无法选中。
IE中的方法:

var radioButton=document.createElement("<input type='radio' name='radioButton' value='checked'>");

为了满足大多数人的需要,同时照顾少数人的利益,我们需要在这里做个判断,判断的基准是document下面一个叫uniqueID的属性,只有IE才能识别这个属性(怎么样,个性吧~)。那么代码就可以这样来写:

if(document.uniqueID){
// Internet Explorer
var radioButton=document.createElement("<input type='radio' name='radioButton' value='checked'>");
}
else {
// Standard Compliant
var radioButton=document.createElement("input");
radioButton.setAttribute("type","radio");
radioButton.setAttribute("name","radioButton");
radioButton.setAttribute("value","checked");
}


好吧,我们现在就先记住这些特例吧,强烈期待浏览器世界的大团结!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值