How to print one specific Javascript variable in HTML?

前端 未结 3 489
故里飘歌
故里飘歌 2021-01-25 16:51

I want to ask how I can print a Javascript variable in HTML form (e.g. output it on the screen)?

Here is my JS code:



        
相关标签:
3条回答
  • 2021-01-25 17:32

    Try this,

    your HTML

    <input type="text" id="logId" />
    <div id="logId1"></div>
    

    js Script

    var howLongIsThis = myPlayer.duration();
    document.getElementById("logId").value=howLongIsThis;
    document.getElementById("logId1").innerHTML=howLongIsThis;
    

    hope this will give you an idea to solve your problem

    0 讨论(0)
  • 2021-01-25 17:37

    Set it to be the innerHTML or value of a html element:

    var howLongIsThis = myPlayer.duration();
    var displayEl = document.getElementById("duration");
    displayEl.innerHTML = howLongIsThis;
    

    or if you want in in a form field:

    displayEl.value = howLongIsThis;
    
    0 讨论(0)
  • 2021-01-25 17:37

    Assuming you have a <div id="example"></div> somewhere in your HTML, you want to run JavaScript after the DOM has loaded which adds the value you want to that div. In jQuery (which you specified in your question you're using), this would simply be:

    $('div#example').html(myPlayer.duration());
    

    Here's a fiddle: http://jsfiddle.net/RyanJW/QjXKL/2/

    0 讨论(0)
提交回复
热议问题