Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中是否有php echo/print的等价物_Javascript_Echo - Fatal编程技术网

javascript中是否有php echo/print的等价物

javascript中是否有php echo/print的等价物,javascript,echo,Javascript,Echo,假设我想从脚本标记内部打印html 像这样的消息来源 <div>foo</div> <script> print('<div>Print this after the script tag</div>'); </script> <div>bar</div> foo 打印(“在脚本标记后打印此文件”); 酒吧 脚本运行后,在浏览器中应类似于此 <div>foo</div> &

假设我想从脚本标记内部打印html

像这样的消息来源

<div>foo</div>
<script>
print('<div>Print this after the script tag</div>');
</script>
<div>bar</div>
foo
打印(“在脚本标记后打印此文件”);
酒吧
脚本运行后,在浏览器中应类似于此

<div>foo</div>
<script>
print('<div>Print this after the script tag</div>');
</script>
<div>Print this after the script tag</div>
<div>bar</div>
foo
打印(“在脚本标记后打印此文件”);
在脚本标记后打印此文件
酒吧
我可以为此编写自己的代码,但因为这在我看来是一个非常简单的问题,我猜要么我遗漏了什么,要么我的思维在某些方面有缺陷,故意忽略了打印

另外,有些关联:我想知道脚本是否(或是否可以)知道它周围的脚本标记。有了这些信息,就可以更容易地找到要注入的打印html代码的位置,前提是它不是非常不受欢迎的

澄清一下:我不需要你为我编写打印函数。我只需要知道实现这一点的本地方法是否存在,我是否错过了它,或者不应该这样做的原因

编辑 我意识到我没有把这个问题想清楚

我把事实弄清楚了,现在几乎一切都正常了。我本来应该提到模板内部需要打印功能——我正在做一个模板引擎实验。我通过将脚本与普通html分离,并将分离的html SAN脚本与脚本输出连接起来,成功地解决了这个问题


在编写代码时,我注意到由于js的异步特性,一切都不会那么顺利。我想我希望能够在模板中实现任何类型的js魔法,就像我在php中一样。似乎在模板中以防傻瓜的方式支持异步代码需要更多的思考。

您需要使用
document.write()

foo
document.write('在脚本标记后打印此文件');
酒吧
请注意,只有在编写文档的过程中,这才有效。呈现文档后,调用
document.write()
将清除文档并开始编写新文档。如果这是您的用例,请参考此问题的其他答案

// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

使用将允许您执行与console.log相同的操作,但它会检查您使用的浏览器是否能够首先使用console.log,以免因兼容性原因(如6等)而出错。

您可以使用document.write,但这不是一个好的做法,它可能会清除整个页面,这取决于它的执行时间

您应该像这样使用
Element.innerHtml

<div>foo</div>
<span id="insertHere"></span>
<div>bar</div>

<script>
document.getElementById('insertHere').innerHTML = '<div>Print this after the script tag</div>';
</script>
foo
酒吧
document.getElementById('insertHere')。innerHTML='在脚本标记'后打印此文件';

您可以使用
文档。编写
甚至
控制台。编写
(这有利于调试)


但最好的选择是使用DOM来更新页面,这是另一种方法:

$('element').html('<h1>TEXT TO INSERT</h1>');
<html>
<head>
    <title>Echo</title>
    <style type="text/css">
    #result{
        border: 1px solid #000000;
        min-height: 250px;
        max-height: 100%;
        padding: 5px;
        font-family: sans-serif;
        font-size: 12px;
    }
    </style>
    <script type="text/javascript" lang="ja">
    function start(){
        function echo(text){
            lastResultAreaText = document.getElementById('result').innerHTML;
            resultArea = document.getElementById('result');
            if(lastResultAreaText==""){
                resultArea.innerHTML=text;
            }
            else{
                resultArea.innerHTML=lastResultAreaText+"</br>"+text;
            }
        }

        echo("Hello World!");
        }
    </script>
</head>
<body onload="start()">
<pre id="result"></pre> 
</body>

回音
#结果{
边框:1px实心#000000;
最小高度:250px;
最大高度:100%;
填充物:5px;
字体系列:无衬线;
字体大小:12px;
}
函数start(){
函数echo(文本){
lastResultAreaText=document.getElementById('result').innerHTML;
resultArea=document.getElementById('result');
如果(lastResultAreaText==“”){
resultArea.innerHTML=文本;
}
否则{
resultArea.innerHTML=lastResultAreaText+“
”+文本; } } 回声(“你好,世界!”); }
您可以使用

function echo(content) {  
    var e = document.createElement("p");
    e.innerHTML = content;
    document.currentScript.parentElement.replaceChild(document.currentScript, e);
}
它将用content参数中的文本替换调用echo函数的当前正在执行的脚本。

From

JavaScript可以以不同的方式“显示”数据:

使用window.alert()写入警报框

使用document.write()写入HTML输出

使用innerHTML写入HTML元素

使用console.log()写入浏览器控制台


我们将在js中创建自己的函数,如echo“Hello world”

函数echo(…s)//rest运算符
{ 
对于(变量i=0;i

注意:
)rest操作符非常喜欢访问一个对象/数组中的多个参数,为了从对象/数组中获取值,我们应该使用for循环迭代整个对象/数组,如上所述,
s
是我刚才保留的名称,您可以编写任何您想要的内容。

Lol'd在第一段。遗憾的是,我不需要这个用于调试目的。糟糕!(对于未来的观众:使用这个,然后在FF、IE或Chrome中打开你的控制台,点击
F12
)。
+
+
在Chrome中比
对我来说更可取。啊,我在firebug的脚本面板中尝试了document.write(),它总是在编写之前清除页面。它似乎在实际的页面代码中工作。谢谢经过一点测试后,当在初始页面呈现外部调用时,它仍然会清除页面。@Pichan:是的,完全正确。在编写文档时,
document.write
写入文档。编写完成后,
document.write
将创建一个新文档并写入其中。(仔细想想,
document.write
无法写入现有的完整文档是有道理的。PHP等价物是在客户端下载整个页面并断开连接后调用
echo
。尽管我承认,创建新文档的行为有点奇怪;如果它是暗示什么也不做。)答案被接受。虽然我没有得到我所需要的,这是最接近一个会猜测我是在与我的偶然含糊和不太好的t
<html>
<head>
    <title>Echo</title>
    <style type="text/css">
    #result{
        border: 1px solid #000000;
        min-height: 250px;
        max-height: 100%;
        padding: 5px;
        font-family: sans-serif;
        font-size: 12px;
    }
    </style>
    <script type="text/javascript" lang="ja">
    function start(){
        function echo(text){
            lastResultAreaText = document.getElementById('result').innerHTML;
            resultArea = document.getElementById('result');
            if(lastResultAreaText==""){
                resultArea.innerHTML=text;
            }
            else{
                resultArea.innerHTML=lastResultAreaText+"</br>"+text;
            }
        }

        echo("Hello World!");
        }
    </script>
</head>
<body onload="start()">
<pre id="result"></pre> 
</body>
function echo(content) {  
    var e = document.createElement("p");
    e.innerHTML = content;
    document.currentScript.parentElement.replaceChild(document.currentScript, e);
}
function echo( ...s ) // rest operator
 { 
   for(var i = 0; i < s.length; i++ ) {

    document.write(s[i] + ' '); // quotes for space

   }

 } 

  // Now call to this function like echo

 echo('Hellow', "World");