本文介绍了打印javascript转义文本时缺少脚本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! <!DOCTYPE html>< html>< body>< p>单击按钮以触发将要执行的功能< / 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> Im将 demo元素innerHtml设置为 Python的转义版本自动警报( JavaScript警报); 。 如果您运行此html并单击 点击我按钮。您可以将结果看到为 Python Auto。脚本标记丢失。为什么会这样呢?为什么脚本标记及其内容丢失了? 解决方案 !!!请参见下面的评论,也请让您的问题更清楚! > 您可以使用&#x3C \x3C >因为某种原因它在转义字符串的其余部分,所以 <!DOCTYPE html> < html> < body> < p>单击按钮以触发一个函数,该函数将在带有id = demo的p元素中输出 Hello World。 < button onclick = myFunction()>点击我< / 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\x20Auto\x20\x3Cscript\x20type\x3D\x22text\x2Fjavascript\x22\x3E\x20alert\x28\x22JavaScript\x20alert\x22\x29\x3B\x20\x3C\x2Fscript\x3E\x20'";}</script></body></html>Im setting "demo" elements innerHtml to the escaped version of Python Auto alert("JavaScript alert"); .If you run this html and click on the "Click me" button. You can see the result as "Python Auto". The script tag is missing. Why this is happening ? How come the script tag and the content of it is missing ? 解决方案 !!!Please see the comment below, also, please make your question a bit more clear!!!Instead of using \x3C you can use &#x3C because somehow it is escaping rest of the string,<!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> 这篇关于打印javascript转义文本时缺少脚本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 12:14
查看更多