是否已经开发了使用setAttribute代替点(.)属性表示法的最佳实践?
例如:
myObj.setAttribute("className", "nameOfClass");
myObj.setAttribute("id", "someID");
or
myObj.className = "nameOfClass";
myObj.id = "someID";
是否已经开发了使用setAttribute代替点(.)属性表示法的最佳实践?
例如:
myObj.setAttribute("className", "nameOfClass");
myObj.setAttribute("id", "someID");
or
myObj.className = "nameOfClass";
myObj.id = "someID";
当前回答
之前的答案都不完整,大多数都包含错误信息。
在JavaScript中有三种访问DOM元素属性的方法。这三种方法都可以在现代浏览器中可靠地工作,只要您了解如何使用它们。
1. element.attributes
元素有一个属性属性,它返回Attr对象的活动NamedNodeMap。此集合的索引可能因浏览器而异。所以,顺序是不能保证的。NamedNodeMap有添加和删除属性的方法(分别是getNamedItem和setNamedItem)。
注意,尽管XML显式区分大小写,但DOM规范要求规范化字符串名称,因此传递给getNamedItem的名称实际上是不区分大小写的。
使用示例:
var div = document.getElementsByTagName('div')[0]; //you can look up specific attributes var classAttr = div.attributes.getNamedItem('CLASS'); document.write('attributes.getNamedItem() Name: ' + classAttr.name + ' Value: ' + classAttr.value + '<br>'); //you can enumerate all defined attributes for(var i = 0; i < div.attributes.length; i++) { var attr = div.attributes[i]; document.write('attributes[] Name: ' + attr.name + ' Value: ' + attr.value + '<br>'); } //create custom attribute var customAttr = document.createAttribute('customTest'); customAttr.value = '567'; div.attributes.setNamedItem(customAttr); //retreive custom attribute customAttr = div.attributes.getNamedItem('customTest'); document.write('attributes.getNamedItem() Name: ' + customAttr.name + ' Value: ' + customAttr.value + '<br>'); <div class="class1" id="main" data-test="stuff" nonStandard="1234"></div>
2. element.getAttribute & element.setAttribute
这些方法直接存在于Element上,不需要访问属性及其方法,但执行相同的功能。
再次注意,字符串名称是不区分大小写的。
使用示例:
var div = document.getElementsByTagName('div')[0]; //get specific attributes document.write('Name: class Value: ' + div.getAttribute('class') + '<br>'); document.write('Name: ID Value: ' + div.getAttribute('ID') + '<br>'); document.write('Name: DATA-TEST Value: ' + div.getAttribute('DATA-TEST') + '<br>'); document.write('Name: nonStandard Value: ' + div.getAttribute('nonStandard') + '<br>'); //create custom attribute div.setAttribute('customTest', '567'); //retreive custom attribute document.write('Name: customTest Value: ' + div.getAttribute('customTest') + '<br>'); <div class="class1" id="main" data-test="stuff" nonStandard="1234"></div>
3.DOM对象上的属性,例如element.id
使用DOM对象上的方便属性可以访问许多属性。给定对象上存在哪些属性取决于对象的DOM节点类型,而与HTML中指定的属性无关。可用的属性定义在DOM对象原型链中的某个地方。因此,所定义的特定属性将取决于您正在访问的Element的类型。
例如,className和id定义在Element上,存在于所有DOM节点上,这些节点是元素,但不包括文本或注释节点。价值的定义更为狭义。它只在HTMLInputElement和它的后代上可用。
注意,JavaScript属性是区分大小写的。尽管大多数属性将使用小写字母,但也有一些是驼峰式的。所以一定要检查说明书。
这个“图表”捕获了这些DOM对象的原型链的一部分。它甚至还没有完成,但它展示了整体结构。
____________Node___________
| | |
Element Text Comment
| |
HTMLElement SVGElement
| |
HTMLInputElement HTMLSpanElement
使用示例:
var div = document.getElementsByTagName('div')[0]; //get specific attributes document.write('Name: class Value: ' + div.className + '<br>'); document.write('Name: id Value: ' + div.id + '<br>'); document.write('Name: ID Value: ' + div.ID + '<br>'); //undefined document.write('Name: data-test Value: ' + div.dataset.test + '<br>'); //.dataset is a special case document.write('Name: nonStandard Value: ' + div.nonStandard + '<br>'); //undefined <div class="class1" id="main" data-test="stuff" nonStandard="1234"></div>
注意:本文解释了HTML规范如何定义属性,以及现代的常青浏览器如何处理属性。当然,有些老的浏览器(IE、Netscape等)没有遵守这个规范,甚至比这个规范更早。如果你需要支持旧的(破损的)浏览器,你需要的信息比这里提供的更多。
其他回答
这是很好的讨论。我曾有过这样的时刻,我希望,或者让我说希望(我可以补充说,是成功的)重新发明轮子,不管它是正方形的。 上面的任何方法都是很好的讨论,所以任何来这里寻找Element属性和属性之间的区别的人。这是我最值钱的一分钱,我的确是费了好大劲才发现的。我会保持简单,所以没有特别的技术术语。
假设我们有一个变量a。我们所习惯的如下。
下面会抛出一个错误,因为它简单地写了它是一种只能有一个属性的对象,即单数左手边对象=单数右手边对象。其他的东西都被忽略,扔进垃圾桶。 let A = 'f'; A.b = 2; console.log (A.b);
谁决定它必须是单数=单数。那些制定JavaScript和html标准的人,这就是引擎工作的方式。
让我们换个例子。
let A = {}; A.b = 2; console.log (A.b);
这一次它工作.....因为我们已经明确地告诉它了,谁决定我们可以在这个例子中告诉它,而不是在以前的例子中。还是那些制定JavaScript和html标准的人。
我希望我们在这个问题上,让它进一步复杂化
let A = {}; A.attribute = {}; A.attribute.b = 5; console.log (A.attribute.b);//将工作 console.log (A.b);//将不起作用
我们所做的是第一层的树和非奇异对象的子层。除非你知道什么是哪里,并调用它,否则它将工作。
这就是HTMLDOM在为每个HTML元素创建解析和绘制DOm树时所发生的事情。每个都有不同级别的属性。有些是预定义的,有些不是。这就是ID和VALUE位发挥作用的地方。 在场景后面,它们在1级属性和sun级属性(属性)之间以1:1的比例映射。因此,改变一个就会改变另一个。这就是对象getter和setter方案的作用。
let A = { attribute :{ id:'', value:'' }, getAttributes: function (n) { return this.attribute[n]; }, setAttributes: function (n,nn){ this.attribute[n] = nn; if(this[n]) this[n] = nn; }, id:'', value:'' }; A.id = 5; console.log(A.id); console.log(A.getAttributes('id')); A.setAttributes('id',7) console.log(A.id); console.log(A.getAttributes('id')); A.setAttributes('ids',7) console.log(A.ids); console.log(A.getAttributes('ids')); A.idsss=7; console.log(A.idsss); console.log(A.getAttributes('idsss'));
如上所示,ELEMENTS有另一组所谓的属性列表属性,它有自己的主要属性。在两者之间有一些预定义的属性,并以1:1的比例映射,例如ID对每个人都是公共的,但value不是,src也不是。当解析器到达这一点时,它只是简单地调出字典,以确定何时出现这样那样的东西。
所有元素都有属性和属性,它们之间的一些项是公共的。在一个国家常见的东西在另一个国家并不常见。
在过去的HTML3时代,我们首先使用html,然后使用JS。现在的日子,它的其他方式,所以已经使用内联onclick成为如此令人憎恶。在HTML5中,有许多属性列表可以作为集合访问,例如类,样式。在过去,颜色是一个属性,现在被移动到css处理不再有效的属性。
元素。attributes是元素属性中的子属性列表。
除非你能改变Element属性的getter和setter,这几乎是不可能的,因为它会破坏所有的功能,通常是不能立即写出来的,只是因为我们定义了一个A.item并不一定意味着Js引擎也会运行另一行函数将它添加到Element.attributes.item中。
我希望这能进一步澄清什么是什么。 为了这个目的,我尝试了element。prototype。setattribute和自定义函数,它只是打破了所有的东西,因为它覆盖了本地的一堆函数,设置属性函数在幕后发挥作用。
在元素上设置属性(例如class)的方法: 1. 埃尔。className = string 2. el.setAttribute(‘类’,字符串) 3.el.attributes.setNamedItem(对象) 4. el.setAttributeNode(节点)
我做了一个简单的基准测试(这里)
似乎setAttributeNode比使用setAttribute快3倍。
所以如果性能是一个问题-使用“setAttributeNode”
两者之间的一个区别是setAttribute,当用于设置<input/>的值时,将使该值成为它所在表单上调用.reset()时的默认值,但.value =不会这样做。
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset
注意,如果调用setAttribute()来设置特定属性的值,则后续调用reset()不会将该属性重置为其默认值,而是将该属性保持setAttribute()调用设置的值。
If the element you are referring to does not already include a Javascript object property for a given attribute (as others have described), then setting that property will not propagate the change back to the DOM, it just adds the named property to the Javascript object, and the DOM ignores it. For example, getting the element mySpan by id and then doing mySpan.class = 'warning' will do nothing, whether or not the span element in question already has a class attribute defined, because mySpan.class is not defined in the Javascript object for a span element. You have to use mySpan.setAttribute('class', 'warning').
然而,第二个细微差别是使用mySpan设置Javascript对象的innerHTML属性。setAttribute("innerHTML", someHTML)不会更新元素的内容。我不知道Javascript是如何捕获mySpan的。innerHTML = something,并调用HTML解析器,但在底层有一些神奇的东西。
来自Javascript: The Definitive Guide,它澄清了一些事情。它注意到HTML文档的HTMLElement对象定义了与所有标准HTML属性对应的JS属性。
因此,您只需要对非标准属性使用setAttribute。
例子:
node.className = 'test'; // works
node.frameborder = '0'; // doesn't work - non standard attribute
node.setAttribute('frameborder', '0'); // works