作者hijkxyzuw (i,j,k) ×(x,y,z)
看板Ajax
标题[问题] nodeValue 和 textContent 有什麽差?
时间Fri Aug 5 12:21:55 2016
最近 javascript 练到 dom 操作。
记得最早看到的是先做 text node ,
再把 text node 加到父节点下。
然後 text node 的内容是用 .nodeValue 存取。
var t = document.createTextNode('hello world! ');
// t.nodeValue == "hello world! "
var h = document.createElement('h1');
h.appendChild(t);
但後来又看到 textContent 属性。
好像可以不做 text node ,
直接加到父节点上就好了。
var h = document.createElement('h1');
h.textContent = 'hello world! ';
查了一下差别,
好像是 nodeValue 是很早期的东西,
是 DOM level 1 。
textContent 是 DOM level 3 。
最扯的 innerHTML 就不提了。
在实用上这两者有什麽差吗?
用 textContent 可以省一些代码量,
也看得比较清楚。
用 createTextNode 好累……。
=====
补问一下,
createElement 要用大写吗?
网路上很多范例都用大写。
HTML 好像早期习惯元素用大写……,
结果现在 html5 都小写。
--
◢◣ ︵︵ █▔◣ █▔█ █▔▔ █▔█ █▆▉ █ █▔█ █◣█ █▔●
◢◤█◣◢◣ ︵︵ █ █ █▁◤ █▁▁ █▁█ ▉▉▉ █ █▁█ █◥█ █ █
梦之大地 逼逼ㄟ四 █▁◤ █ █ █▁▁ █ █ ▉▉▉ █▁ █ █ █ █ █▁◤
※ Origin: <bbs.ccns.ncku.edu.tw> ◆ From: 140.116.102.204
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.116.102.135
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Ajax/M.1470370918.A.981.html
※ 编辑: hijkxyzuw (140.116.102.135), 08/05/2016 12:27:49