Print console.log on HTML [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-14 03:37:00

问题


I've a javascript code that is currently working on a shared hosting.

The script gather data from a website and then show it on my website.

Fact is that the data is being showed in the console tab but not on the php page. The script itself contains that code:

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

... </script>

But I don't understand how to print the "followersSum" function to display it on the HTML.

Does someone can help me?


回答1:


Let's say you have a <div id="printFollower"></div> somewhere in your html.

Then do this:

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

Using this method, you can control where the list shows up better, apply CSS, etc.




回答2:


I suggest you use 'document.write'

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

This will print the text straight into the DOM rather than the console. For more information on document, check out the docs




回答3:


Change the text content of some element in your document

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



回答4:


You can do document.write() it will just print it onto the page.

    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>


来源:https://stackoverflow.com/questions/45132192/print-console-log-on-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!