替换document.write [英] replacement for document.write

查看:70
本文介绍了替换document.write的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,我想要在那里的日期.以下代码包括document.write.因此,文本将出现在纯文档中.如何避免这种情况并使文本显示在div中? div的ID为出现".

I have a div and I want the date in there. The following code includes document.write. So the text will appear in a plain document. How can I avoid this and let the text appear in the div? The div has the id "appear".

<script type="text/javascript">

var date=new Date();
var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

document.write(month[day.getMonth()]);

</script> 

推荐答案

例如,可以使用所有元素都具有的属性innerHTML来实现.
You can do it using the property innerHTML which all elements have, for example.
myDiv = document.GetElementById("myDivId");
myDiv.innerHTML = month[day.getMonth()]; // you can add any HTML markup here


您需要使用匹配的id属性标记目标div:


You need to mark the target div with matching id attribute:

<div id="myDivId"></id>



首先,您可以在此div中放入一些内容或将其保留为空.如果将由上面显示的JavaScript代码片段填充.

—SA



At first, you can put some content inside this div or leave it empty. If would be filled in by the fragment of JavaScript code shown above.

—SA


这篇关于替换document.write的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆