我有一个javascript代码,目前正在共享主机上工作。
该脚本从网站收集数据,然后将其显示在我的网站上。
事实是,数据显示在控制台选项卡中,但不显示在 php 页面上。脚本本身包含该代码:
<script> ... function printFollowers(followersSum) { console.log(followersSum); // you can do print followersSum wherever you want }
... </script>
但是我不明白如何打印"followersSum"函数以将其显示在HTML上。
有人可以帮助我吗?
假设你在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 等。
我建议你使用'document.write'
function printFollowers(followersSum) {
document.write(followersSum);
}
printFollowers(prompt("Write some text"));
这会将文本直接打印到 DOM 而不是控制台中。 有关文档的更多信息,请查看文档
更改文档中某些元素的文本内容
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" />
你可以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>