开发者

Iexplorer 9 - jscript - replace backgroundImage

I'am using the next line in a js script with FF and IE9. The BackgroundImage is set somewhere to

url("images/\\BalkLinks.png")

and later I like to change that image to one with shadow.

stbmkloon.childNodes[x].style.backgroundImage=stbmkloon.childNodes[x].style.backgroundImage.replace(".png","S.png");

In FF, IE8 this is going well but in IE9 the /\\ is changed into a slash forward, a strange character and "Bal" is dropped. Also ".png" is replaced by开发者_如何学Python "S.png" what is correct.

Is there somebody who can tell me what i'm doing wrong ?

Gr

an example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">

function PersoonOpVoorgrond (naam)
{
alert('before :'+document.getElementById(naam).style.backgroundImage);
            document.getElementById(naam).style.backgroundImage=document.getElementById(naam).style.backgroundImage.replace(/.png/,"S.png");
alert('after :'+document.getElementById(naam).style.backgroundImage);

}
</SCRIPT>


</head>

<body>
    <div id="Switch" onclick="PersoonOpVoorgrond('Switch');" style="background-image:url(images/\\BalkLinks.png); background-position:0% 0%; background-repeat: no-repeat; border:2px black solid; width:50px;">
        Switch
    </div>
</body>
</html>


You should use regular expressions in the replace function.

backgroundImage.replace(/.png/g,"S.png");

There is also one thing that is dangerous: Not all browsers return the current value of a style property. For example chrome would return an empty string for "style.backgroundImage".

Chrome returns only values that are specified by JavaScript before.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜