简体   繁体   English

如何在HTML中打印一个特定的Javascript变量?

[英]How to print one specific Javascript variable in HTML?

I want to ask how I can print a Javascript variable in HTML form (eg output it on the screen)? 我想问一下如何以HTML格式打印Javascript变量(例如在屏幕上输出)?

Here is my JS code: 这是我的JS代码:

<script type="text/javascript">
var howLongIsThis = myPlayer.duration();
</script>

This var howLongIsThis = myPlayer.duration(); 这个var howLongIsThis = myPlayer.duration(); is Jquery variable! 是jQuery变量!

So how i can show this value in HTML page? 那么我如何在HTML页面中显示此值?

Set it to be the innerHTML or value of a html element: 将其设置为innerHTML或html元素的值:

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

or if you want in in a form field: 或者如果您想在表单字段中输入:

displayEl.value = howLongIsThis;

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 . 假设您在HTML中的某个位置有一个<div id="example"></div> ,则您想在DOM加载后运行JavaScript,该JavaScript将您想要的值添加到该div In jQuery (which you specified in your question you're using), this would simply be: 在jQuery(您在问题中使用的jQuery中指定)中,它就是:

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

Here's a fiddle: http://jsfiddle.net/RyanJW/QjXKL/2/ 这是一个小提琴: http : //jsfiddle.net/RyanJW/QjXKL/2/

Try this, 尝试这个,

your HTML 您的HTML

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

js Script js脚本

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 希望这会给您一个解决问题的想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM