javascript replace() 函数在 Internet Explorer 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8285559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
replace() function not working in Internet Explorer
提问by LEOPM
I have a js replace function to replace text next to two radio buttons on a pre set form.
我有一个 js 替换功能来替换预设表单上两个单选按钮旁边的文本。
Script is as follows.
脚本如下。
document.body.innerHTML = document.body.innerHTML.replace(
"Payment by <b>Moneybookers</b> e-wallet<br>", ""
);
document.body.innerHTML = document.body.innerHTML.replace(
"Maestro, Visa and other credit/debit cards by <b>Moneybookers</b>",
"Pago con Diners Club, Mastercard o Visa"
);
The script works fine in Chrome and Firefox, however, the script is not actioned in Explorer.
该脚本在 Chrome 和 Firefox 中运行良好,但是,该脚本在资源管理器中不起作用。
I believe it has something to do with there being , / -
within the text I am replacing? When I use the function to replace text with no , / -
in the text - it works fine in explorer, however, for example when I try to replace text.. - "Maestro, Visa and other credit/debit cards by Moneybookers" this does not work in explorer.. I'm assuming because of the coma and forward slash. Honestly I've tried everything but just can not get this to work. Any help would be greatly appreciated!!
我相信这与/ -
在我要替换的文本中存在 , 有关系?当我使用该功能将文本替换为 no 时,/ -
在文本中 - 它在资源管理器中工作正常,但是,例如当我尝试替换文本时.. - “Maestro、Visa 和 Moneybookers 的其他信用卡/借记卡”这不会在资源管理器中工作.. 我假设是因为昏迷和正斜杠。老实说,我已经尝试了所有方法,但无法使其正常工作。任何帮助将不胜感激!!
回答by nickf
Try it with a regular expression:
用正则表达式试试:
.replace(/Payment by <b>Moneybookers<\/b> e-wallet<br>/, "")
If that doesn't work, try making it case insensitive. Internet Explorer might be converting the tags to upper case.
如果这不起作用,请尝试使其不区分大小写。Internet Explorer 可能会将标签转换为大写。
.replace(/Payment by <b>Moneybookers<\/b> e-wallet<br>/i, "")
On a different point, replacing the entire body's innerHTML probably isn't a great idea. Every DOM element in the page would be destroyed, the new HTML re-parsed and then new elements created. Perhaps try to narrow down your replacement to the element which actually contains this text.
另一方面,替换整个 body 的 innerHTML 可能不是一个好主意。页面中的每个 DOM 元素都会被销毁,重新解析新的 HTML,然后创建新元素。也许尝试将替换范围缩小到实际包含此文本的元素。
回答by Zach
I recently fixed a bug that might help.
我最近修复了一个可能有帮助的错误。
If your value that you were replacing was 0 -- as a value, not a string -- IE11 would only append the replacement string instead of actually replacing it.
如果您要替换的值是 0——作为一个值,而不是一个字符串——IE11 只会附加替换字符串而不是实际替换它。
Here's what I was working with:
这是我正在使用的内容:
buf = buf.replace( /%%TOTALAMOUNT%%/gim, "$" + parseFloat( g_UserPurchases[LCV].CurrencyValue.val() ).toFixed(2) );
This printed: "%%TOTALAMOUNT%%.00"
这打印:“ %%TOTALAMOUNT%%.00”
I fixed it by checking:
我通过检查修复了它:
if( ( g_UserPurchases[LCV].CurrencyValue.val() == 0 ) || ( g_UserPurchases[LCV].CurrencyValue.val() === 0 ) ){
//IE fix: IE did not like the $ character and didn't replace if val = 0
buf = buf.replace( /%%TOTALAMOUNT%%/gim, "$0.00"); }
Please note: IE11 didn't replace the the dollar sign character, $. So, I used the character code instead:
请注意:IE11 没有替换美元符号字符 $。所以,我改用了字符代码:
$
Hope this helps!
希望这可以帮助!