admin管理员组文章数量:1399217
So while working with the dom, I came across the situation where I assumed that the object resulting from document.createTextNode()
would be treated in a similar way to an object resulting from document.createElement();
, in that I would be able to call setAttribute()
on it.
Example:
var genericElementNode = document.createElement('p');
genericElementNode.setAttribute('id', 'sampleId1');
// The above will run fine
var textNode = document.createTextNode("Hello World");
textNode.setAttribute('id', 'sampleId2');
//The above will result in an error:
//Uncaught TypeError: textNode.setAttribute is not a function
Why is this the case? And are there any workarounds?
So while working with the dom, I came across the situation where I assumed that the object resulting from document.createTextNode()
would be treated in a similar way to an object resulting from document.createElement();
, in that I would be able to call setAttribute()
on it.
Example:
var genericElementNode = document.createElement('p');
genericElementNode.setAttribute('id', 'sampleId1');
// The above will run fine
var textNode = document.createTextNode("Hello World");
textNode.setAttribute('id', 'sampleId2');
//The above will result in an error:
//Uncaught TypeError: textNode.setAttribute is not a function
Why is this the case? And are there any workarounds?
Share Improve this question asked Oct 20, 2016 at 9:54 RugnirRugnir 2405 silver badges12 bronze badges 3-
5
Text nodes are not elements, and therefore have a different prototype. ---
setAttribute
is a method ofHTMLElement
, whichtextNode
does not inherit from. – evolutionxbox Commented Oct 20, 2016 at 9:55 - "are there any workarounds?" What is your end goal? – James Thorpe Commented Oct 20, 2016 at 10:01
- @evolutionxbox It would be excellent if you could expand that into an answer. Also, do you have any idea why textNode is not a 'node', despite being named as such. Why isn't it called something more appropriate? – Rugnir Commented Oct 20, 2016 at 10:19
2 Answers
Reset to default 7you cant set/get any of attributes/elements of textNode
are there any workarounds?
it's easy to say creating element inside such as span
and set your text
var genericElementNode = document.createElement('p');
genericElementNode.setAttribute('id', 'sampleId1');
// The above will run fine
var textNode = document.createElement("span");
textNode.innerText = "Hello World";
textNode.setAttribute('id', 'sampleId2');
document.createTextNode
returns exactly that... a Text Node, which inherits from Node
. This does not have the setAttribute
method.
An Element
, which can be created using document.createElement
, also inherits from Node
, but it also has a bunch more methods including setAttribute
.
As @Mamdouh Freelancer has stated, you can instead use an inline level element such as a span
.
var textNode = document.createElement('span');
textNode.textContent = 'Hello world';
textNode.setAttribute('id', 'sampleId2');
本文标签: javascriptWhy does documentcreateTextNode() not allow setAttribute()Stack Overflow
版权声明:本文标题:javascript - Why does document.createTextNode() not allow setAttribute()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744211577a2595442.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论