我的应用程序读取 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);
最佳答案
您没有在任何地方保存该值!
styleValue = styleValue.replace("raymarine", "XXX");
styleValue = styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue = styleValue.replace("view=thumb", "view=png");
如果您愿意,可以通过字符串替换来使其成为单行:
styleValue = styleValue.replace("raymarine", "XXX").replace("ShowImage", "ShowImageSystemCreator").replace("view=thumb", "view=png");
关于javascript 方法替换未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33348993/