简体   繁体   English

replace()函数在Internet Explorer中不起作用

[英]replace() function not working in Internet Explorer

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. 当我使用该功能以no替换文本时, / -在文本中-在资源管理器中可以正常工作,例如,当我尝试替换文本时。在探险家中不起作用..我假设是因为昏迷和正斜线。 Honestly I've tried everything but just can not get this to work. 老实说,我已经尝试了一切,但只是无法让这个工作。 Any help would be greatly appreciated!! 任何帮助将不胜感激!!

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. 另一方面,替换整个人的innerHTML可能不是一个好主意。 Every DOM element in the page would be destroyed, the new HTML re-parsed and then new elements created. 将破坏页面中的每个DOM元素,重新解析新的HTML,然后创建新元素。 Perhaps try to narrow down your replacement to the element which actually contains this text. 也许尝试将替换范围缩小到实际包含此文本的元素。

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, "&#36;0.00");    }

Please note: IE11 didn't replace the the dollar sign character, $. 请注意:IE11没有替换美元符号字符$。 So, I used the character code instead: 因此,我改用字符代码:

&#36;

Hope this helps! 希望这可以帮助!

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

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