在 HTML 上打印 console.log

新手上路,请多包涵

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

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

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

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

... </script>

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

有人可以帮助我吗?

原文由 Andrew 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 457
2 个回答

假设您的 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 等。

原文由 Difster 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

 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" />

原文由 user2314737 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题