[英]JavaScript Using the DOM methods and properties
Hey guys i have some code here that is supposed to create HTML elements on the page that will display today date next to ID='today' How do use a textnode to write out to the ID? 大家好,我在这里有一些代码可以在页面上创建HTML元素,这些元素将在ID ='today'旁边显示今天的日期。如何使用textnode写入ID? Thanks for any help!
谢谢你的帮助! Here is my Full code:
这是我的完整代码:
HTML: HTML:
<body>
<h1>ToDo List - Date: <span id='today'> </span></h1>
<div id="todolist">
<p>
<input type="button" id="additem" value="Add Item">
</p>
</div>
<hr>
<div>
<p>
<input type="button" id="sortitems" value="Sort and Display Items">
</p>
<p id="displayitems">
</p>
</div>
</body>
Javascript: 使用Javascript:
var $ = function (id){
return document.getElementById(id);
}
var TodaysDate = function () {
var myDate = new Date();
var myMonth = myDate.getMonth() + 1;
var myDay = myDate.getDate();
var myYear = myDate.getFullYear();
var myResponse = myMonth + "/" + myDay + "/" + myYear;
var myPara = document.createElement("p");
var myP = document.getElementById('today');
myP.appendChild(myPara);
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
}
window.onload = function ()
{
$("today").onload = TodaysDate;
$("sortitems").onclick = sortItem;
$("additems").onclick = addItem;
}
Span onload? 跨度加载? Onload is not triggered for a span.
跨度未触发上载。 Other object do have a load event for example: windows/frames, body, link, script and images
其他对象确实具有加载事件,例如:窗口/框架,主体,链接,脚本和图像
Change 更改
$("today").onload = TodaysDate;
To 至
TodaysDate();
and 和
var myPara = document.createElement("p");
var myP = document.getElementById('today');
myP.appendChild(myPara);
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
To 至
var myPara = document.createElement("p");
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
var myP = $('today');
myP.appendChild(myPara);
Try replacing this line: 尝试替换此行:
$("today").onload = TodaysDate;
with this line: 用这一行:
TodaysDate();
because the SPAN
tag does not support the onload
event (see this answer) . 因为
SPAN
标记不支持onload
事件(请参见此答案) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.