简体   繁体   English

iexplorer 9-JScript-替换backgroundImage

[英]Iexplorer 9 - jscript - replace backgroundImage

I'am using the next line in a js script with FF and IE9. 我在FF和IE9的js脚本中使用下一行。 The BackgroundImage is set somewhere to BackgroundImage设置为

url("images/\\\\BalkLinks.png") 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. 在FF中,IE8进行得很好,但在IE9中,/ \\\\变成了斜杠,一个奇怪的字符,并且删除了“ Bal”。 Also ".png" is replaced by "S.png" what is correct. 同样,“。png”也将替换为“ S.png”。

Is there somebody who can tell me what i'm doing wrong ? 有人可以告诉我我做错了什么吗?

Gr 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. 您应该在replace函数中使用正则表达式。

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将为“ style.backgroundImage”返回一个空字符串。

Chrome returns only values that are specified by JavaScript before. Chrome仅返回JavaScript之前指定的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM