简体   繁体   中英

document.querySelector(“foreignObject”) is null in Chrome

Working with embedded SVG in HTML5 I've found strange behavior in Chrome browser. ( http://jsfiddle.net/complynx/htp4hqe2/ )

For example, in the following html/svg code:

<svg>
    <foreignObject>
        <body xmlns="http://www.w3.org/1999/xhtml">
            <div>foo</div>
        </body>
    </foreignObject>
</svg>
<script>
    var T=document.querySelector("foreignObject");
</script>

Variable T will be null in Chrome (for Firefox works fine). Any other selectors, even for contents of <foreignObject> work fine.

Is there any tag-specific selector in Chrome for this case?

Upd : As Rob W mentioned in comments, there is a known bug in WebKit.

Simple, yet in some cases not good workaround.

<svg>
    <foreignObject class="ForeignObjectStubClass">
        <body xmlns="http://www.w3.org/1999/xhtml">
            <div>foo</div>
        </body>
    </foreignObject>
</svg>
<script>
    var T=document.querySelector(".ForeignObjectStubClass");
</script>

Using CSS class instead of tag name is enough

Edit, Updated

Try

var _T = document.getElementsByTagName("foreignObject");
// select `DIV` element within `_T` `HTMLCollection`
var filtered = _T[0].children.item("DIV");
console.log(filtered);

jsfiddle http://jsfiddle.net/guest271314/htp4hqe2/2/

See ParentNode.children , HTMLCollection ; see also NodeFilter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM