Iexplorer 9-jscript-替换backgroundImage

| 我在FF和IE9的js脚本中使用下一行。 BackgroundImage设置为 url(\“ images / \\\\ BalkLinks.png \”) 然后我喜欢将该图像更改为带有阴影的图像。
stbmkloon.childNodes[x].style.backgroundImage=stbmkloon.childNodes[x].style.backgroundImage.replace(\".png\",\"S.png\");
在FF中,IE8进行得很好,但在IE9中,/ \\\\变为正斜杠,一个奇怪的字符和\“ Bal \”被删除。同样,\“。png \”替换为\“ S.png \”是正确的。 有人可以告诉我我做错了什么吗? GR 一个例子:
<!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>
    
已邀请:
您应该在replace函数中使用正则表达式。
backgroundImage.replace(/.png/g,\"S.png\");
还有一件事很危险: 并非所有浏览器都返回样式属性的当前值。例如,chrome将为\“ style.backgroundImage \”返回一个空字符串。 Chrome仅返回JavaScript之前指定的值。     

要回复问题请先登录注册