XMLSerializer 跳过元素值 [英] XMLSerializer skips element values
问题描述
我有一些 Javascript 代码可以序列化整个 DOM 树,如下所示:
I've got some Javascript code which serializes the entire DOM tree as follows:
(new XMLSerializer()).serializeToString(document)
但是我得到的与我预期的有些不同.具体来说,我得到了 HTML 源代码,但没有得到各种输入/选择的值.如果对象被插入到 DOM 中,我会得到这些对象,但同样没有值.
But what I get is somewhat different than I'd expect. Specifically, I get the HTML source, but not the values of the various input/selects. If objects are inserted into the DOM, I get those objects, but again, no values.
但是,如果我为了其他目的在 Javascript 中遍历 DOM,我将可以访问每个输入的值.
However, if I were to walk through the DOM in Javascript for some other purpose, I would have access to every input's value.
什么给?显然,我缺少一些非常基本的概念,但我不知道是什么.
What gives? Obviously, I'm missing some very basic concept, but I've no idea what.
我可以获取完整的 DOM 作为 HTML/XML 格式的字符串,带有输入/选择值吗?如果是这样,如何?
Can I get the full DOM as an HTML/XML formatted string, with input/select values? If so, how?
推荐答案
这是因为 DOM 表单输入维护其value
属性(这反映了当前状态)与其 value
attribute(表示输入的初始值,由 XMLSerializer
用来序列化 DOM)分开.
This is due to the fact that a DOM form input maintains its value
property (which reflects the current state) separately from its value
attribute (which represents the initial value of the input and is used by XMLSerializer
to serialize the DOM).
我在 Stack Overflow 上多次写过这方面的文章.这是最近的一个例子: jQuery .attr('value', 'new_value') 不工作?
I've written about this several times on Stack Overflow. Here's a recent example: jQuery .attr('value', 'new_value') not working?
这篇关于XMLSerializer 跳过元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!