<!DOCTYPE html>
<html>
<body>
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "'Python\x20Auto\x20\x3Cscript\x20type\x3D\x22text\x2Fjavascript\x22\x3E\x20alert\x28\x22JavaScript\x20alert\x22\x29\x3B\x20\x3C\x2Fscript\x3E\x20'";
}
</script>
</body>
</html>
最佳答案
需要使用 < 来表示 <,使用 > 来表示 >,使用\"来表示 "。其他一切都应该可以使用实际字符,见下文。
<!DOCTYPE html>
<html>
<body>
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "'Python Auto <script type=\"text/javascript\"> alert(\"JavaScript alert\");</script>'";
}
</script>
</body>
</html>
关于javascript - 打印 javascript 转义文本时缺少脚本标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40422236/