【问题标题】:difference between using getAttributeNode and setAttributeNode使用 getAttributeNode 和 setAttributeNode 的区别
【发布时间】:2015-09-23 05:49:01
【问题描述】:

我想知道使用getAttributeNode和setAttributeNode的区别:

var x = document.getElementsByTagName("IMG")[0];
x.getAttributeNode("src").value = "pic_bulbon.gif";
x.setAttributeNode("src").value = "pic_bulbon.gif";

【问题讨论】:

  • 在名称中,一个sets 是属性,另一个get 是属性。它们在您上面的代码中没有被正确使用。

标签: javascript attributes tags


【解决方案1】:

Element.getAttributeNode()返回指定元素的指定属性,作为Attr节点。

Element.setAttribute()用于添加新属性或更改指定元素上现有属性的值。

var x = document.getElementsByTagName("IMG")[0]; // Get the first image element
console.log(x.getAttributeNode("src")); // Returns the src value of image
x.setAttributeNode("src", "pic_bulbon.gif"); // Set src value of image

【讨论】:

    猜你喜欢
    • 2015-11-24
    • 1970-01-01
    • 2015-03-12
    • 2019-04-02
    • 2010-11-09
    • 2017-01-30
    • 2010-10-26
    • 2021-09-20
    • 2012-01-01
    相关资源
    最近更新 更多