[英]how can i pass a parameters to a function to get current Style
美好的一天,我想知道如何在IE中獲取currentStyle,並將參數傳遞給這樣的函數參數:
function test(el,value){
return document.getElementById(el).currentStyle[value];
}
如果我要使用類似的功能從Firefox,Chrome等獲取樣式,則可能會導致這種情況。
使用這樣的功能:
function test(el,value){
return getComputedStyle(document.getElementById(obj))[value];
}
,其中value是元素屬性,例如backgroundColor,即:
alert(test('ObjectId','backgroundColor'));
....它將在FF,Chrome ..中返回backgroundColor,但在Internet Explorer中不返回
有什么可能的解決方案..?
Thnx ..
請我不是在尋找jQuery解決方案...
這是我用來檢索樣式屬性值的內容
function getStyle(el,sProp,toInt){
var elem = el;
if (elem.currentStyle) {
return toInt
? parseInt(elem.currentStyle[sProp],10)
: elem.currentStyle[sProp] || 0;
} else if (window.getComputedStyle) {
var compStyle = window.getComputedStyle(elem, null)[sProp];
return toInt ? parseInt(compStyle,10) : compStyle || 0;
}
return String(elem.style[sProp]||0);
}
這(非常)復雜。
我已經編寫了與瀏覽器無關的解析器,但無法與您共享。
除非您要編寫自己的框架,否則我必須問,為什么要能夠解決所有問題? 是否需要特定的屬性(或某些屬性)? 因為那可能容易得多。
如果只需要背景顏色,那么.style.backgroundColor可能就足夠了。
此外,示例腳本中還有一個錯誤:
alert(test('ObjectId'),'backgroundColor');
應該:
alert(test('ObjectId','backgroundColor'));
我不是第一次犯同樣的錯誤;)-花了我半天的時間找到它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.