gpt4 book ai didi

javascript - HTML 脚本标签可以包含在 jsFiddle 的 Javascript 变量中吗?

转载 作者:行者123 更新时间:2023-11-30 08:58:02 25 4
gpt4 key购买 nike

我试图用一个 jsFiddle 示例在 SO 上回答用户的问题。该示例是动态初始化 iframe通过 Javascript 元素。 iframe要显示的内容是分配给变量的有效 html 文档。这是变量赋值:

var aValidDoc = '<!DOCTYPE html PUBLIC
\"-//W3C//DTD XHTML 1.0
Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html
xmlns=\"http://www.w3.org/1999/xhtml\">
<head><title></title><style
type=\"text/css\">@media
screen{html,body{margin:0;padding:0;height:100%;width:100%}p{margin:15px;}}</style>
</head><body><p>This is the content of
the dynmic document.</p><body></html>';

这是我想做的事情的片段:

</p><script type=\"text/javascript\">alert(\"Hi\")</script><body></html>';

事实上,即使是 <script> 的评论打破接口(interface):

// below line breaks jsFiddle
// <script type="text//javascript"></script>

这是 fiddle :jsFiddle example

有没有办法编写变量赋值来包含script标记以免破坏 jsFiddle 接口(interface)?

最佳答案

字面上写</script>内联 javascript 中的任何位置都会破坏包含的 html 标记。

一个简单的解决方法是编写 "<\/script>"反而。因为它在字符串文字中,反斜杠被删除,你得到 "</script>"

http://jsfiddle.net/KavDy/2/

关于javascript - HTML 脚本标签可以包含在 jsFiddle 的 Javascript 变量中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11749786/

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