热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Javascript:在HTML中转义双引号-Javascript:EscapingdoublequotesinHTML

HowcanIpreventimages[i].titlebelowfrombreakingtheHTMLifitcontainsdoublequotes?如果它包含双引

How can I prevent images[i].title below from breaking the HTML if it contains double quotes?

如果它包含双引号,我如何防止图像[i] .title破坏HTML?

for ( i=0;i<=images.length-1;i++ ){
    gallery += '';
}

4 个解决方案

#1


6  

You can use the replace() method to escape the double quotes:

您可以使用replace()方法来转义双引号:

for (var i = 0; i ';
}

EDIT: The result will be a valid Javascript string, but won't work as HTML markup because the HTML parser doesn't understand backslash escapes. You'll either have to replace double quote characters with single quotes in your image title:

编辑:结果将是一个有效的Javascript字符串,但不会作为HTML标记工作,因为HTML解析器不理解反斜杠转义。您必须在图像标题中用单引号替换双引号字符:

for (var i = 0; i ';
}

Or invert the quote types in your markup:

或者反转标记中的报价类型:

for (var i = 0; i ";
}

#2


13  

Since no one seems to have exactly the right answer in my opinion:

因为在我看来,没有人似乎有正确的答案:

for ( i=0;i<=images.length-1;i++ ){
    gallery += '';
}

This replaces all quotes, and you end up with double-quotes, and they are represented in an html format that is valid.

这将取代所有引号,最后使用双引号,它们以有效的html格式表示。

#3


2  

var_name.replace(/\"/gi,'%22');

That's the one you're looking for. Even if your colors look "off" in Visual Studio.

这就是你要找的那个。即使您的颜色在Visual Studio中看起来“关闭”。

\ escapes the following quote. gi does a replace for all occurences.

\逃脱以下引用。 gi取代所有出现的事物。

#4


1  

You can call replace on your title string:

您可以在标题字符串上调用replace:

for ( i=0;i<=images.length-1;i++ ){
    gallery += '';
}

推荐阅读
author-avatar
徐曼曼_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有