小码哥的IT人生

HTML DOM Abbreviation 对象

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

HTML DOM Abbreviation 对象

Abbreviation 对象

Abbreviation 对象代表 HTML <abbr> 元素。

访问 Abbreviation 对象

您可使用 getElementById() 来访问 <abbr> 元素:

示例代码:

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

完整实例:

<!DOCTYPE html>
<html>
<body>
<h3>如何访问 ABBR 元素的演示</h3>
<p>The <abbr id="myAbbr" title="World Health Organization">WHO</abbr> was founded in 1948.</p>
<p>单击按钮可获取“WHO”的缩写。</p>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
  var x = document.getElementById("myAbbr").title;
  document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

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

创建 Abbreviation 对象

您可使用 document.createElement() 方法来创建 <abbr> 元素:

示例代码:

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

完整实例:

<!DOCTYPE html>
<html>
<body>
<p>单击该按钮来创建包含缩写 "WHO" 的 ABBR 元素。</p>
<button onclick="myFunction()">试一试</button>
<p id="myP"></p>
<script>
function myFunction() {
  var x = document.createElement("ABBR");
  var t = document.createTextNode("WHO");
  x.setAttribute("title", "World Health Organization");
  x.appendChild(t);
  document.getElementById("myP").appendChild(x);
}
</script>
</body>
</html>

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

标准属性和事件

Abbreviation 对象支持标准属性事件

相关页面

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

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

苏公网安备 32030202000762号

© 2021-2024