JavaScript:使用document.write();

时间:2015-06-09 01:50:40

标签: javascript

我正在尝试使用document.write INSIDE中的另一个document.write来创建新的HTML文档。

我知道这是一种痛苦的方法(并且可以使用所需的输出重写代码),但这里是:

var newDoc = document.open("text/html", "replace");
                newDoc.write("<html><head><title>Failure!</title></head><body><h5>js::error 5015 &&syn.0</h5><p>In Quantum Mechanisms exists a quantum state known as quantum superposition - the state of being two things at once given a random subatomic event that may or may not occur.</p><p>A famous experiment by Erwin Schrödinger, a cat was placed in a box along with a poison flask that would kill the cat, only activating if a subatomic change was detected (which is completely random). The thought experiment brought forth the idea of the cat being <em>dead</em> and <em>alive</em> at the same exact time, but you aren't able to know which it is. So in theory, by opening the box and finding that the cat is dead, you have essentially killed the cat yourself.</p><p>Quantum Mechanics have been applied to the button you just clicked. The choice was yours on whether or not to click it, and you did not know where you would end up. You can say that it would have unlocked a new upgrade, or break the game, but since you clicked the button, you therefore must have broken the game yourself. Just like that, curiosity killed the cat. Sorry, quantum superposition is pretty terrible. Fortunately, you can click this button below to continue on your journey.</p><button onclick=\"var newDoc = document.open(\"text/html\", \"replace\"); newDoc.write(\"<html><head><title>Double Fail</title></head><body><p>No seriously, quantum mechanics are pretty bad. :)</p></body></html>\"); newDoc.close();\">Continue</button></body></html>");
                newDoc.close();

如您所见,按钮没有清楚地输出信息。不知道我做错了什么。

2 个答案:

答案 0 :(得分:2)

许多逃避需要发生:

var newDoc = document.open("text/html", "replace");
                newDoc.write('<html><head><title>Failure!</title></head><body><h5>js::error 5015 &&syn.0</h5><p>In Quantum Mechanisms exists a quantum state known as quantum superposition - the state of being two things at once given a random subatomic event that may or may not occur.</p><p>A famous experiment by Erwin Schrödinger, a cat was placed in a box along with a poison flask that would kill the cat, only activating if a subatomic change was detected (which is completely random). The thought experiment brought forth the idea of the cat being <em>dead</em> and <em>alive</em> at the same exact time, but you aren\'t able to know which it is. So in theory, by opening the box and finding that the cat is dead, you have essentially killed the cat yourself.</p><p>Quantum Mechanics have been applied to the button you just clicked. The choice was yours on whether or not to click it, and you did not know where you would end up. You can say that it would have unlocked a new upgrade, or break the game, but since you clicked the button, you therefore must have broken the game yourself. Just like that, curiosity killed the cat. Sorry, quantum superposition is pretty terrible. Fortunately, you can click this button below to continue on your journey.</p><button onclick="var newDoc = document.open(\'text/html\', \'replace\'); newDoc.write(\'<html><head><title>Double Fail</title></head><body><p>No seriously, quantum mechanics are pretty bad. :)</p></body></html>\'); newDoc.close();">Continue</button></body></html>');
                newDoc.close();

以下是问题的开始:

</p><button onclick=\"var newDoc = document.open(\"text/html\"
                     ^^ starting onclick         ^^ ending it

答案 1 :(得分:0)

var newDoc = document.open("text/html", "replace");
newDoc.write("<html><head><title>Failure!</title></head><body>Button:<button onclick=\"var newDoc = document.open(&quot;text/html&quot;, &quot;replace&quot;); newDoc.write(&quot;<html><head><title>Double Fail</title></head><body><p>Button clicked.</p></body></html>&quot;); newDoc.close();\">Continue</button></body></html>");
newDoc.close();

你的报价很糟糕。生成的HTML将在"中断,您需要在&quot;属性中使用HTML转义(onclick),而不是在此时使用JavaScript转义(\")。