javascript innerHTML 没有子节点?

发布于 2024-08-29 13:58:00 字数 1737 浏览 3 评论 0原文

我遇到了 Firefox 问题,我看不到树木的木材 使用ajax,我从php脚本中获取html源代码,

此html代码包含一个标签,并且在tbody中还有更多tr/td,

现在我想将此tbody纯代码附加到现有表中。但还有一个条件:表格是表单的一部分,因此包含复选框和下拉列表。如果我会使用 table.innerHTML += content; firefox 重新加载表格并重置其中的所有元素,这不是很用户友好,因为 id 喜欢拥有

我拥有的东西,

// content equals transport.responseText from ajax request
function appendToTable(content){
    var wrapper = document.createElement('table');
    wrapper.innerHTML = content;
    wrapper.setAttribute('id', 'wrappid');
    wrapper.style.display = 'none';
    document.body.appendChild(wrapper);

    // get the parsed element - well it should be
    wrapper = document.getElementById('wrappid');
    // the destination table
    table = document.getElementById('tableid');

    // firebug prints a table element - seems right
    console.log(wrapper);
    // firebug prints the content ive inserted - seems right
    console.log(wrapper.innerHTML);

    var i = 0;
    // childNodes is iterated 2 times, both are textnode's
    // the second one seems to be a simple '\n'
    for(i=0;i<wrapper.childNodes.length;i++){
        // firebug prints 'undefined' - wth!??
        console.log(wrapper.childNodes[i].innerHTML);
        // firebug prints a textnode element - <TextNode textContent=" ">
        console.log(wrapper.childNodes[i]);
        table.appendChild(wrapper.childNodes[i]);
    }
    // WEIRD: firebug has no problems showing the 'wrappid' table and its contents in the html view - which seems there are the elements i want and not textelements
}

这要么是微不足道的,我没有看到问题,要么 这是一个极端的情况,我希望这里有人有足够的经验对此提出建议 - 任何人都可以想象为什么我得到文本节点而不是我期望的最终解析的 dom 元素?

顺便说一句:顺便说一句,我无法给出完整的示例,因为我无法编写较小的非工作代码 这是在野外发生的错误之一,而不是在我的测试集中,

谢谢大家

im having a firefox issue where i dont see the wood for the trees
using ajax i get html source from a php script

this html code contains a tag and within the tbody some more tr/td's

now i want to append this tbody plaincode to an existing table. but there is one more condition: the table is part of a form and thus contains checkboxe's and drop down's. if i would use table.innerHTML += content; firefox reloads the table and reset's all elements within it which isnt very userfriendly as id like to have

what i have is this

// content equals transport.responseText from ajax request
function appendToTable(content){
    var wrapper = document.createElement('table');
    wrapper.innerHTML = content;
    wrapper.setAttribute('id', 'wrappid');
    wrapper.style.display = 'none';
    document.body.appendChild(wrapper);

    // get the parsed element - well it should be
    wrapper = document.getElementById('wrappid');
    // the destination table
    table = document.getElementById('tableid');

    // firebug prints a table element - seems right
    console.log(wrapper);
    // firebug prints the content ive inserted - seems right
    console.log(wrapper.innerHTML);

    var i = 0;
    // childNodes is iterated 2 times, both are textnode's
    // the second one seems to be a simple '\n'
    for(i=0;i<wrapper.childNodes.length;i++){
        // firebug prints 'undefined' - wth!??
        console.log(wrapper.childNodes[i].innerHTML);
        // firebug prints a textnode element - <TextNode textContent=" ">
        console.log(wrapper.childNodes[i]);
        table.appendChild(wrapper.childNodes[i]);
    }
    // WEIRD: firebug has no problems showing the 'wrappid' table and its contents in the html view - which seems there are the elements i want and not textelements
}

either this is so trivial that i dont see the problem OR
its a corner case and i hope someone here has that much of expirience to give an advice on this - anyone can imagine why i get textnodes and not the finally parsed dom elements i expect?

btw: btw i cant give a full example cause i cant write a smaller non working piece of code
its one of those bugs that occure in the wild and not in my testset

thx all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦忆晨望 2024-09-05 13:58:00

您可能遇到了 Firefox 遵循 W3C 规范的怪癖。在规范中,标签之间的空白是“文本”节点而不是元素。这些 TextNode 在 childNode 中返回。 这个其他答案 描述了一种解决方法。使用 JQuery 之类的东西也可以使这变得更容易。

You are probably running into a Firefox quirk of following the W3C spec. In the spec the whitespace between tags are "text" nodes instead of elements. These TextNodes are returned in childNodes. This other answer describes a workaround. Also Using something like JQuery makes this much easier.

删除→记忆 2024-09-05 13:58:00

我希望在任何浏览器中都会出现这种行为,因为 += 操作数会根据定义覆盖表中已有的内容。两种解决方案:

让 PHP 生成要添加到表中的项目列表,而不是从 PHP 文件接收 HTML 代码。逗号/制表符分隔,无论如何。然后使用 Table.addRow()、Row.addCell() 和 cell.innerHTML 将项目添加到表中。这是我建议这样做的方式,在两个单独的文件中创建 GUI 元素是没有意义的。

另一种解决方案是将已输入的所有表单数据保存到本地 JavaScript 变量中,附加表,然后将数据重新加载到表单字段中。

I would expect this behavior in any browser as the += operand overwrites what is already in the table by definition. Two solutions:

Instead of receiving HTML code from your PHP file, have the PHP generate a list of items to add to the table. Comma/tab separated, whatever. Then use Table.addRow(), Row.addCell() and cell.innerHTML to add the items to the table. This is the way I would suggest doing it, no point in creating GUI elements in two separate files.

The other solution is to save all the form data that's already been entered to local JavaScript variables, append the table, and then reload the data into the form fields.

生死何惧 2024-09-05 13:58:00

好吧,返回带有新数据的 JSON 对象似乎是最好的选择。然后,您可以使用它来合成额外的表格元素。

如果被迫获取纯 HTML 作为响应,可以使用 var foo = document.createElement('div');,例如,然后执行 foo.innerHTML =响应文本;。这将创建一个未附加到任何内容的元素,但托管已解析的 HTML 响应。
然后,您可以深入了解 foo 元素,获取所需的元素,并以 DOM 友好的方式将它们附加到表中。

编辑:

好吧,我想我现在明白你的观点了。
wrapper 本身就是一个表格元素。这些节点位于 tbody 下,tbody 是 wrapper 的子级,tbody 是其 lastChild (或者您可以通过其 tBodies[0] 成员访问它,在火狐浏览器中)。
然后,使用 tBody 元素,我认为您将能够得到您想要的。

顺便说一句,在将其子项附加到表之前,您不需要将包装器附加到文档,因此无需隐藏它等。

Well, returning a JSON object with the new data seems like the best option. Then, you can synthesize the extra table elements by using it.

In case one is forced to get plain HTML as response, it is possible to use var foo = document.createElement('div');, for example, and then do foo.innerHTML = responseText;. This creates an element that is not appended to anything, yet hosts the parsed HTML response.
Then, you can drill down the foo element, get the elements that you need and append them to the table in a DOM-friendly fashion.

Edit:

Well, I think I see your point now.
wrapper is a table element itself. The nodes reside under the tbody, a child of wrapper which is its lastChild (or you can access it via its tBodies[0] member, in Firefox).
Then, using the tBody element, I think that you would be able to get what you want.

BTW, You do not need to append the wrapper to the document before appending its children to the table, so no need to hide it etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文