gpt4 book ai didi

带有比较符号的 JavaScript 字符串会被忽略吗?

转载 作者:行者123 更新时间:2023-11-28 17:45:51 24 4
gpt4 key购买 nike

我注意到一个以前从未遇到过的问题。 JavaScript 中包含 < 的字符串被忽略。这是为什么?以下是我注意到的一些结果:

msg = "<This entire string would be ignored."
msg = "This part of the string is ok. <While this part is ignored."
msg = "<This part is ignored> But this half of the string will still print."

这是某种特殊的转义字符还是什么?它是干什么用的?此外,解决此问题的方法是什么?

这里是代码:

var msg = "This is my message <This is ignored";
document.write(msg);

最佳答案

这不是 JavaScript 的问题;这是你的代码的问题。这就是问题所在,正如 @Tomalak 所建议的。

您正在使用innerHTMLtextContent 相反,例如,它将尝试将字符串解析为带有属性的标签。

<This part is ignored><this>part , is ,和ignored属性

关于带有比较符号的 JavaScript 字符串会被忽略吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46742054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com