getElementsByClassName returns [] instead of asynchronous appended node

后端 未结 6 1879
野性不改
野性不改 2020-12-17 05:40

(I ask my question again after the first one was terribly formulated)

I face the following problem:

相关标签:
6条回答
  • 2020-12-17 06:11

    colorSelector is commented out. JavaScript only works within the DOM, and commented out portions aren't in the DOM.

    0 讨论(0)
  • 2020-12-17 06:17

    Since you said that your getElementById("0-0") is successful, then clearly you don't actually have the nodes commented out.

    I'm guessing you're doing:

    document.getElementById("0-0").getElementsByClassName('colorSelector');
    

    ...which will not work because the element selected by ID does not have any descendants with that class.


    Since you show HTML comments in the markup, I'd also wonder if you have some different element on the page with the ID "0-0". Take a look for that.


    If your nodes are actually commented out, you'll need to first select the comment, and replace it with the markup contained inside:

    var container = document.getElementById('test1'),
        comment = container.firstChild;
    
    while( comment && comment.nodeType !== 8 ) {
        comment = comment.nextSibling;
    }
    
    if( comment ) {
        container.innerHTML = comment.nodeValue;
    }
    

    ...resulting in:

    <div class="testA" id="test1">
        <div class="colorSelector" id="0-0">
            <div class="gbSelector" id="1-0">
                <table style="none" id="2-0"></table>
            </div>
        </div>
    </div>
    

    ...but there again, this doesn't seem likely since your getElementsById does work.

    0 讨论(0)
  • 2020-12-17 06:19

    I now append the first div

    How do you do that? What you have in the responseXML are XML elements, and not HTML elements.

    • You shouldn't be able to appendChild them into a non-XHTML HTML document;

    • actually you shouldn't be able to appendChild them into another document at all, you're supposed to use importNode to get elements from one document to another, otherwise you should get WRONG_DOCUMENT_ERR;

    • even if you managed to insert them into an HTML due to browser laxness, they're still XML elements and are not semantically HTML elements. Consequently there is nothing special about the class attributes; just having that name doesn't make the attribute actually represent a class. getElementsByClassName won't return elements just because they have attributes with the name class; they have to be elements whose language definition associates the attributes with the concept of classness (which in general means HTML, XHTML or SVG).

    (The same should be true of the id attributes; just having an attribute called id doesn't make it conceptually an ID. So getElementById shouldn't be working. There is a way to associate arbitrary XML attributes with ID-ness, which you don't get with class-ness, by using an <!ATTLIST declaration in the doctype. Not usually worth bothering with though. Also xml:id is a special case, in implementations that support XML ID.)

    You could potentially make it work if you were using a native-XHTML page by putting suitable xmlns attributes on the content to make it actual-XHTML and not just arbitrary-XML, and then using importNode. But in general this isn't worth it; it tends to be simpler to return HTML markup strings (typically in JSON), or raw XML data from which the client-side script can construct the HTML elements itself.

    0 讨论(0)
  • 2020-12-17 06:28
    <!--<div class="colorSelector" id="0-0">
            <div class="gbSelector" id="1-0">
                <table style="none" id="2-0"></table>
            </div>
        </div>-->
    

    The above code is gray for a reason: it's a comment. Comments aren't parsed by the browser at all and have no influence on the page whatsoever.

    You'll have to parse the HTML, read the comments, and make a new DOM object with the contents of the comment.

    0 讨论(0)
  • 2020-12-17 06:30

    Please describe what you are doing with the returned results. There is a significant difference between a nodeList and a node, nodeLists are LIVE.

    So if you assign a nodeList returned by getElementsByClassName() (or similar) to a variable, this variable will change when you remove the nodes inside the nodeList from the DOM.

    0 讨论(0)
  • 2020-12-17 06:32

    Here's a way to do it for Firefox, Opera, Chrome and Safari. Basically, you just do div.innerHTML = div.innerHTML to reinterpret its content as HTML, which will make that class attribute from the XML file be treated as an HTML class name.

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title></title>
            <script>
                window.addEventListener("DOMContentLoaded", function() {
                    var div = document.getElementsByTagName("div")[0];
                    var req = new XMLHttpRequest();
                    req.onreadystatechange = function() {
                        if (this.readyState === 4 && this.status === 200) {
                            var doc = this.responseXML;
                            div.appendChild(document.importNode(doc.getElementsByTagName("response")[0].getElementsByTagName("div")[0], true));
                            div.innerHTML = div.innerHTML;
                            alert(document.getElementsByClassName("colorSelector").length);
                        }
                    };
                    req.open("GET", "div.xml");
                    req.send();
                }, false);
            </script>
        </head>
        <body>
            <div class="testA"></div>
        </body>
    </html>
    

    Remove the this.status === 200 if you're testing locally in browsers that support xhr locally.

    The importNode() function doesn't seem to work in IE (9 for example). I get a vague "interface not supported" error.

    You could also do it this way:

    var doc = this.responseXML;
    var markup = (new XMLSerializer()).serializeToString(doc.getElementsByTagName("response")[0].getElementsByTagName("div")[0]);
    div.innerHTML = markup;
    

    as long as the markup is HTML-friendly as far as end tags for empty elements are concerned.

    0 讨论(0)
提交回复
热议问题