在 HTML 上打印 console.log

Print console.log on HTML

我有一个 javascript 代码目前正在共享主机上工作。

脚本从网站收集数据,然后在我的网站上显示。

事实是数据显示在控制台选项卡中,而不是 php 页面上。脚本本身包含该代码:

<script> ... function printFollowers(followersSum) {   console.log(followersSum); // you can do print followersSum wherever you want }

... </script>

但我不明白如何打印 "followersSum" 函数以在 HTML 上显示它。

有人可以帮助我吗?

您可以 document.write() 它只会将其打印到页面上。

    function printFollowers(followersSum) {
      followersSum = document.getElementById('printFollower').innerHTML
      console.log(followersSum); // you can do print followersSum wherever you want
      document.write(followersSum);
    }

    printFollowers();
<div id="printFollower">1</div>

我建议你使用'document.write'

function printFollowers(followersSum) {
  document.write(followersSum);
}
printFollowers(prompt("Write some text"));

这会将文本直接打印到 DOM 而不是控制台。 有关文档的更多信息,check out the docs

假设您的 html 中某处有一个 <div id="printFollower"></div>

然后这样做:

function printFollowers(followersSum) {
  document.getElementById('printFollower').innerHTML = followersSum;
  console.log(followersSum); // you can do print followersSum wherever you want
}

使用此方法,您可以更好地控制列表显示的位置、应用 CSS 等

更改文档中某些元素的文本内容

function printFollowers(followersSum) {
  document.getElementById('myId').textContent = followersSum;
  console.log(followersSum); // you can do print followersSum wherever you want
}

var followersSum = 0;
printFollowers("followersSum = " + followersSum);
<div id="myId" />