小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JavaScript中setAttribute用法

 右子龍 2011-01-19

我們經(jīng)常需要在JavaScript中給Element動(dòng)態(tài)添加各種屬性,這可以通過使用setAttribute()來實(shí)現(xiàn),這就涉及到了瀏覽器的兼容性問題。

setAttribute(string name, string value):增加一個(gè)指定名稱和值的新屬性,或者把一個(gè)現(xiàn)有的屬性設(shè)定為指定的值。

1、樣式問題
setAttribute("class", value)中class是指改變"class"這個(gè)屬性,所以要帶引號。
vName代表對樣式賦值。
例如:
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("name", "q");
input.setAttribute("class",bordercss);
輸出時(shí):<input type="text" name="q" class="bordercss">,即,input控件具有bordercss樣式屬性
注意:class屬性在W3C DOM中扮演著很重要的角色,但由于瀏覽器差異性仍然存在。
使用setAttribute("class", vName)語句動(dòng)態(tài)設(shè)置Element的class屬性在firefox中是行的通的,但在IE中卻不行。因?yàn)槭褂肐E內(nèi)核的瀏覽器不認(rèn)識(shí)"class",要改用"className";
同樣,firefox 也不認(rèn)識(shí)"className"。所以常用的方法是二者兼?zhèn)洌?/p>

element.setAttribute("class", value); //for firefox
element.setAttribute("className", value); //for IE

2、方法屬性等問題
例如:
var bar = document.getElementById("testbt");
bar.setAttribute("onclick", "javascript:alert('This is a test!');");
這里利用setAttribute指定e的onclick屬性,簡單,很好理解。
但是IE不支持,IE并不是不支持setAttribute這個(gè)函數(shù),而是不支持用setAttribute設(shè)置某些屬性,例如對象屬性、集合屬性、事件屬性,也就是說用setAttribute設(shè)置style和onclick這些屬性在IE中是行不通的。

為達(dá)到兼容各種瀏覽器的效果,可以用點(diǎn)符號法來設(shè)置Element的對象屬性、集合屬性和事件屬性。
document.getElementById("testbt").className = "bordercss";
document.getElementById("testbt").style.cssText = "color: #00f;";
document.getElementById("testbt").style.color = "#00f";
document.getElementById("testbt").onclick= function () { alert("This is a test!"); }


文章來源:http://hi.baidu.com/underwear_clothes/blog/item/200036377d1ff2daa3cc2ba7.html


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多