首页 > 解决方案 > 为什么我的字符串中的尖括号输出不正确?

问题描述

我在字符串中使用尖括号时遇到了困难。我正在尝试输出文件,但显然我不知道如何处理。公平地说,我对这一切都处于一个拖拉的水平,所以我的语言可能是错误的(我是一名作家/视觉设计师;代码不是我的事)。

基本上,如果有东西在尖括号中,我会遇到以下几个问题之一:

我尝试使用 > 和 < 代替 < 和 > 来查看是否可以解决问题,但它只会使用“>”和“<”。

我正在使用这个:

function download(filename, text) {
    var element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    element.setAttribute('download', filename);
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
}

尝试转动这样的东西:

"#\n# Configuration stuff below\n# (Based on buttons selected)\n#\n\n Single word items in tags like <this> create another instance of the item as if to close it manually.\n";
"\n\n <extension type>\n Two words in an tag results in added characters.\n </extension>";
"\n\n <input type>\n Use of the word \"input\" in a tag makes the closing tag straight up vanish.\n </input>";

...变成这样的:

#
# Configuration stuff below
# (Based on buttons selected)
#

Single word items in tags like <this> create another instance of the item as if to close it manually.

 <extension type>
 Two words in an tag results in added characters.
 </extension>

 <input type>
 Use of the word "input" in a tag makes the closing tag straight up vanish.
 </input>

但相反,我得到了这个:

#
# Configuration stuff below
# (Based on buttons selected)
#

 Single word items in tags like <this> create another instance of the item as if to close it manually.
</this>

 <extension type="">
 Two words in an tag results in added characters.
 </extension>

 <input type="">
 Use of the word "input" in a tag makes the closing tag straight up vanish.

我正在尝试的精简版本在这里: https ://codepen.io/anon/pen/PoYGmpo

纠正标签中项目问题的最佳方法是什么(没有及时返回并将我的艺术学位变成更实用的东西)?

标签: javascript

解决方案


推荐阅读