javascript方法替换不能按预期工作

时间:2015-10-26 15:01:44

标签: javascript replace

我的应用程序读取DOM对象的属性值。我希望这个值与一些新文本交换并放回属性。原始值是:

"position: absolute; background-image: url(\"http://dealer.raymarine.com/Views/Public/ShowImage.ashx?partno=M81203&view=thumb\")";

但是什么时候应该用JS替换方法更新,但没有改变,为什么?

更新JS代码:

var styleValue = ui.helper[0].getAttribute("style");
styleValue.replace("raymarine", "XXX");
styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue.replace("view=thumb", "view=png");
ui.helper[0].setAttribute("style", styleValue);
console.log("draggable after text swap: " + styleValue);

1 个答案:

答案 0 :(得分:3)

你没有在任何地方保存价值!

styleValue = styleValue.replace("raymarine", "XXX");
styleValue = styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue = styleValue.replace("view=thumb", "view=png");

如果您想通过串联replace s:

,请将其设为一行
styleValue = styleValue.replace("raymarine", "XXX").replace("ShowImage", "ShowImageSystemCreator").replace("view=thumb", "view=png");
相关问题