简体   繁体   English

为什么将textContent分配给引用不会在引用更改时更新DOM?

[英]Why assigning textContent to a reference does not update the DOM when reference is changed?

It looks like textContent is a property in Node, but why the below code doesn't work as expected: 看起来textContent是Node中的一个属性,但为什么下面的代码不能按预期工作:

http://jsbin.com/ukexab/1/edit http://jsbin.com/ukexab/1/edit

var d = document.getElementById("container");

var data = {text: "something different"};

d.textContent = data.text;

data.text = "new text";

Is this something like copying and not referencing? 这是复制而不是引用吗?

Yes, it's due to how assignment works. 是的,这是由于作业如何运作。

Assigning an Object or anything derived from it to a variable will actually assign a reference to that object, and increase the object's reference count. Object或从中派生的任何内容分配给变量实际上将分配对该对象的引用,并增加对象的引用计数。

Assigning a primitive will pass a copy of its value. 分配基元将传递其值的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么单击时链接会从 DOM 中删除,而不仅仅是更改 textContent? - Why does a link gets removed from the DOM when clicked instead of just changing textContent? 为什么将变量分配给“textContent”元素必须位于页面底部? - why does assigning a variable to 'textContent' element have to be at bottom of page? 为什么即使对象引用值发生变化并且应该执行 break 语句时,while 循环仍会继续循环? - Why does the while loop keeps looping even when the object reference value changed and the break statement should execute? 每个都在jQuery中更新DOM引用 - Update DOM reference in jQuery each 为什么在页面更改时DOM自动删除我的类? - Why does the DOM remove my classes automatically when the page is changed? DOM 改变不更新点击事件 - DOM changed does not update click event 为什么用textContent链接无法正常工作 - Why linkify with textContent does not work 分配给不带var关键字的变量时,为什么会出现参考错误? - Why do I get a reference error when assigning to a variable without the var keyword? 为对象分配其他内容时,为什么不通过引用传递此对象? - Why isn't this object being passed by reference when assigning something else to it? 参考改变前参考改变 - Reference changed before change of reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM