Javascript 如何从java脚本中的n级嵌套document.write()打印
我有以下代码Javascript 如何从java脚本中的n级嵌套document.write()打印,javascript,html,dom,Javascript,Html,Dom,我有以下代码 <body> <script> (function(s) { }) ("document.write('<script>document.write(\" <script>document.write('<script>document.write(\"Hello World\");<\/script>');<\/script>\");<\/
<body>
<script>
(function(s) { })
("document.write('<script>document.write(\" <script>document.write('<script>document.write(\"Hello World\");<\/script>');<\/script>\");<\/script>')");
</script>
</body>
(职能{})
(“document.write(\”document.write(\”document.write(\”Hello World\”);\”));
在函数体中,我应该编写什么代码,以便这里的代码能够解析和打印Hello World”。这里的解决方案必须适用于n级document.write()
嵌套。
坦率地说,我不知道怎么做。
有人能为我提供解决方案吗?对不起,我认为这没有意义,因为您只尝试嵌套“document.write()” 调用“document.write()”将导致整个文档被write参数覆盖。 但是javascript引擎不会再次解析内容,因此即将到来的嵌套代码不会运行 也许除了document.write()之外还有其他用途可以匹配。但我不知道这种情况 如果您试图在JS中嵌套函数,请尝试
function foo(doBar)
{
function bar()
{
console.log( 'bar' );
}
function baz()
{
console.log( 'baz' );
}
window.baz = baz;
if ( doBar ) bar();
}
嵌套的document.write()
//(函数){在此处编写代码以解析并打印“Hello World”。请记住-您的解决方案必须适用于n级的document.write()嵌套}(“document.write(\”document.write(\”document.write(\”document.write(\”Hello World\”);\”));
(功能){
s=“document.write(\”document.write(\”document.write(\”Hello World\”;\”;\”)”);
var str,scriptEle=document.createElement('script');
str=s.replace(/[“]/g,”);
str=str.replace(/“document.write/g,”);
str=str.replace(/;“/g,”);
scriptEle.innerHTML=str;
document.body.appendChild(scriptEle);
})();
Hello,为什么要执行嵌套的“document.write()”调用?据我所知,您的调用将被嵌套的“document.write()”覆盖,也许使用的目的更清楚地说明了如何编写代码。这是什么语法方法?;)嵌套的document.write()”
表示document.write('document.write(\'document.write(\'document.write(\'Hello World\');\”);\”))”);
document.write()在document.write()中好吧,这样行得通,因为javascript解析器不会解析嵌套的代码。或者我错了吗?它行得通,但我不知道怎么做。这就是我发布这个问题的原因。它行得通,但我不知道怎么做。这就是我发布这个问题的原因
<!doctype html>
<html>
<head>
<title>Nested document.write()</title>
</head>
<body>
<script>
//(function(s) { write your code here to parse s and print "Hello World". Remember - your solution must work for n level of document.write() nesting })( "document.write('<script>document.write(\"<script>document.write('<script>document.write(\"Hello World\");<\/script>');<\/script>\");<\/script>')");
(function(s){
s = "document.write('<script>document.write(\"<script>document.write('<script>document.write(\"Hello World\");<\/script>\");<\/script>\");<\/script>')";
var str, scriptEle = document.createElement('script');
str = s.replace(/["']/g, '"');
str = str.replace(/"<script>document.write/g, "");
str = str.replace(/;<\/script\>"/g, "");
scriptEle.innerHTML = str;
document.body.appendChild(scriptEle);
})();
</script>
</body>
</html>