小码哥的IT人生

HTML DOM Time 对象

JavaScript基础 2022-05-13 16:23:40小码哥的IT人生shichen

HTML DOM Time 对象

Time 对象

Time 对象是 HTML5 中的新对象。

Time 对象表示 HTML <time> 元素。

访问 Time 对象

您可以通过使用 getElementById() 来访问 <time> 元素:

var x = document.getElementById("mySource");

完整实例:

<!DOCTYPE html>
<html>
<body>
<h3>演示如何访问 TIME 元素</h3>
<p>我在<time id="myTime" datetime="2014-02-14">情人节</time>有个约会。</p>
<p>点击按钮来获得以上 time 元素的日期。</p>
<p id="demo"></p>
<button onclick="myFunction()">试一下</button>
<p><b>注释:</b>time 元素不会在浏览器中呈现任何特殊的样式。</p>
<p><b>注释:</b>只有 Firefox 和 Opera 12 支持 dateTime 属性。</p>
<script>
function myFunction()
{
var x = document.getElementById("myTime").dateTime;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

创建 Time 对象

您可以通过使用 document.createElement() 方法来创建 <time> 元素:

var x = document.createElement("SOURCE");

完整实例:

<!DOCTYPE html>
<html>
<body>
<h3>演示如何创建 TIME 元素</h3>
<p>点击按钮来创建规定情人节日期(2014-02-14)的 TIME 元素。</p>
<button onclick="myFunction()">试一下</button>
<script>
function myFunction()
{
var x = document.createElement("TIME");
x.setAttribute("datetime", "2014-02-14");
var t = document.createTextNode("情人节");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

Time 对象属性

属性 描述
dateTime 设置或返回 <time> 元素中 datetime 属性的值。

标准属性和事件

Time 对象支持标准属性事件

相关页面

HTML 参考手册:HTML <time> 标签

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024