javascript如何获取html样式的自定义属性值

时间:2022-11-27 10:34:02

I have html element with some inline styles plus on custom css property. now i can access every style attribute but not my custom style property.

我有一些带有内联样式的html元素以及自定义css属性。现在我可以访问每个样式属性但不能访问我的自定义样式属性。

here is my code.

这是我的代码。

<img id="myimg" class="ImgClass" style="overfolow:hidden;position:absolute;mycustomProp:100;top:15;left:20">

here i cannot get mycustomProp.

在这里我无法获得mycustomProp。

Any help?

3 个解决方案

#1


0  

http://jsfiddle.net/phrjwgxk/

alert(getCustomStyle("myimg","mycustomProp"));


function getCustomStyle(theId,theStyle) {
    var styles=document.getElementById(theId).getAttribute("style").split(';');
    var astyle;
    for(var i=0;i<styles.length;i++) {
        astyle=styles[i].split(':');
        if(astyle[0]==theStyle) return (astyle[1]);
    }
    return undefined;
}

#2


1  

document.getElementById("myimg").getAttribute("style") to retrieve the attribute.

document.getElementById(“myimg”)。getAttribute(“style”)来检索属性。

document.getElementById("myimg").setAttribute("style",<string>) can be used to change it.

document.getElementById(“myimg”)。setAttribute(“style”, )可用于更改它。

#3


0  

check

$("#myDiv").hasAttribute(name)

get

$("#myDiv").getAttribute(name)

set

$("#myDiv").setAttribute(name)

delete

$("#myDiv").removeAttribute(name)

#1


0  

http://jsfiddle.net/phrjwgxk/

alert(getCustomStyle("myimg","mycustomProp"));


function getCustomStyle(theId,theStyle) {
    var styles=document.getElementById(theId).getAttribute("style").split(';');
    var astyle;
    for(var i=0;i<styles.length;i++) {
        astyle=styles[i].split(':');
        if(astyle[0]==theStyle) return (astyle[1]);
    }
    return undefined;
}

#2


1  

document.getElementById("myimg").getAttribute("style") to retrieve the attribute.

document.getElementById(“myimg”)。getAttribute(“style”)来检索属性。

document.getElementById("myimg").setAttribute("style",<string>) can be used to change it.

document.getElementById(“myimg”)。setAttribute(“style”, )可用于更改它。

#3


0  

check

$("#myDiv").hasAttribute(name)

get

$("#myDiv").getAttribute(name)

set

$("#myDiv").setAttribute(name)

delete

$("#myDiv").removeAttribute(name)