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 += '<img width="250" height="250" src="' + images[i].src + '" title="' + images[i].title + '" />';
}
4 个解决方案
#1
6
You can use the replace() method to escape the double quotes:
您可以使用replace()方法来转义双引号:
for (var i = 0; i < images.length; ++i) {
gallery += '<img width="250" height="250" src="' + images[i].src
+ '" title="' + images[i].title.replace(/\"/g, '\\"') + '" />';
}
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 < images.length; ++i) {
gallery += '<img width="250" height="250" src="' + images[i].src
+ '" title="' + images[i].title.replace(/\"/g, "'") + '" />';
}
Or invert the quote types in your markup:
或者反转标记中的报价类型:
for (var i = 0; i < images.length; ++i) {
gallery += "<img width='250' height='250' src='" + images[i].src
+ "' title='" + images[i].title + "' />";
}
#2
13
Since no one seems to have exactly the right answer in my opinion:
因为在我看来,没有人似乎有正确的答案:
for ( i=0;i<=images.length-1;i++ ){
gallery += '<img width="250" height="250" src="' + images[i].src
+ '" title="' + images[i].title.replace(/\"/g,'"') + '" />';
}
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 += '<img width="250" height="250" src="' + images[i].src + '" title="' + images[i].title.replace('"',"'") + '" />';
}
#1
6
You can use the replace() method to escape the double quotes:
您可以使用replace()方法来转义双引号:
for (var i = 0; i < images.length; ++i) {
gallery += '<img width="250" height="250" src="' + images[i].src
+ '" title="' + images[i].title.replace(/\"/g, '\\"') + '" />';
}
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 < images.length; ++i) {
gallery += '<img width="250" height="250" src="' + images[i].src
+ '" title="' + images[i].title.replace(/\"/g, "'") + '" />';
}
Or invert the quote types in your markup:
或者反转标记中的报价类型:
for (var i = 0; i < images.length; ++i) {
gallery += "<img width='250' height='250' src='" + images[i].src
+ "' title='" + images[i].title + "' />";
}
#2
13
Since no one seems to have exactly the right answer in my opinion:
因为在我看来,没有人似乎有正确的答案:
for ( i=0;i<=images.length-1;i++ ){
gallery += '<img width="250" height="250" src="' + images[i].src
+ '" title="' + images[i].title.replace(/\"/g,'"') + '" />';
}
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 += '<img width="250" height="250" src="' + images[i].src + '" title="' + images[i].title.replace('"',"'") + '" />';
}