HTML JavaScript获得textNode的ID标记名什么? [英] HTML Javascript get textNode by id tagname anything?

查看:147
本文介绍了HTML JavaScript获得textNode的ID标记名什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,要将id分配给textNode,然后通过该id检索该文本节点?我尝试了几种不同的方法,并不断收到错误,说不能得到null的属性。



我的代码看起来像这样...

  var myDiv = document.createdElement('div'); 
myDiv.id =textContainer;

var textNode = document.createdTextNode(some text);
textNode.id =descriptionText;
myDiv.appendChild(textNode);

此时可以正常工作;它会显示在页面上。

  var tempNode = document.getElementById(descriptionText ); 
descriptionText.value =新文字;

这是不行的。我尝试过使用tagName,data等文本节点命名的变量,而且我遇到了同样的错误。那么,命名和检索文本节点是不可能的?而创建新文本节点并替换旧文本节点的下一个最佳解决方案?

解决方案

文本没有id属性,所以当你将节点写入DOM时,它是不能再这样访问。您在JavaScript中创建的内容( Text 对象)不直接与DOM元素相关,如 HTMLElement

事实上,它甚至不被认为是父级的孩子,它是父级的 textContent 。请注意, textContent 来自继承自 Node



您可以为对象分配一个id当你创建它时,因为所有的对象都只是对象,但是当你把它放到DOM中时,所有非标准的东西都会消失。

如果你测试父元素儿童:

  myDiv.children.length; 

您会看到(如果没有其他孩子),您添加的文本节点是吸收到父母作为财产。



这里有一点 jsfiddle 展示。






注意:这是对文本的过度简化在浏览器中处理。我不知道我是否称之为粗俗过度简化,但它是一种方式。



需要注意的是,文本 CharacterData ,它又继承自 Node 。但是,这些是接口,它们限制了可用方法/属性的范围。



另外,文本节点 始终可以在DOM中访问,但不能通过标识符或标签访问。 Node.normalize的MDN页面 清楚地说明了这些节点是如何通过 childNodes 只读 nodeList



尽管将这些东西放在后袋中很有用,在节点和日常使用的正常化和CharacterData环境中考虑文本并不重要。


Is there anyway to assign an id to a textNode and then retrieve that text node via that id? I've tried several different ways and keep getting errors saying cannot get property of null.

My code looks something like this...

 var myDiv = document.createdElement('div');
 myDiv.id = "textContainer";

 var textNode = document.createdTextNode("some text");
 textNode.id = "descriptionText";
 myDiv.appendChild(textNode);

Works fine to this point; it displays on the page. Later I try to modify that text node and that's where I'm getting the errors.

 var tempNode = document.getElementById(descriptionText);
 descriptionText.value = "new text";

And it's a no go. I've tried variates like naming by text node with tagName, data, etc., and I'm getting the same error. So, is it not possible to name and retrieve a text node? And the next best solution to create new text nodes and replace the old text node?

解决方案

Text doesn't have a property for id, so when you write the node into the DOM, it's not accessible that way any more. What you create in the JavaScript (a Text object) does not directly correlate to a DOM element like HTMLElement.

In fact, it's not even considered a child of the parent, it's the textContent of the parent. Note that textContent comes from an element inheriting from a Node.

You can assign an "id" to the object when you create it because all objects are just objects, but when you put it into the DOM, all non-standard stuff disappears.

If you test your parent element for it's children:

myDiv.children.length;

You'll see that (if there are no other children) the text node you've added is "absorbed" into the parent as a property.

Here's a little jsfiddle demonstrating.


Side note: this is an over-simplification of how text is handled in the browser. I don't know if I'd call it a gross over-simplification, but it is one either way.

It should be noted that Text inherits from CharacterData which in turn inherits from Node. However, these are interfaces, which limit the scope of available methods / properties.

Additionally, Text nodes are always accessible in the DOM, but not via identifiers or tags. The MDN page for Node.normalize clearly demonstrates how these nodes are available via the childNodes read-only nodeList.

While it is useful to keep this stuff in your back pocket, it's probably not important to think about Text in the context of Nodes and normalization and CharacterData for day to day use.

这篇关于HTML JavaScript获得textNode的ID标记名什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆