IFRAME中的instanceof HTMLElement不是Element或Object? [英] instanceof HTMLElement in IFRAME is not Element or Object?

查看:201
本文介绍了IFRAME中的instanceof HTMLElement不是Element或Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过简单检查尝试确定DOM元素

Trying determinate DOM Element by simple check

isElement = SomeThing instanceof Element

适用于主文档,但不适用于iframe中的(所有?)节点。

works in main document, but not on (all?) nodes in iframe.

示例输出(谷歌浏览器):( mdiv是主文档中的DIV,idiv是iframe中的DIV)

Example output (Google Chrome): (mdiv is DIV in main document, idiv is DIV in iframe)

OMGWTF 
ok:  mdiv instanceof Element ... true ... [object HTMLDivElement]
ok:  mdiv instanceof Object ... true ... [object HTMLDivElement]
ko:  idiv instanceof Element ... false ... [object HTMLDivElement]
KO :  idiv instanceof Object ... false ... [object HTMLDivElement] 

主文档和iframe文档有不同的javascript实现???

There are different javascript implementations for main document and for iframe documents ???

请解释我的错误。

示例:( http://www.sbmintegral.sk/GITHUB/OMGWTF/obalka.xhtml

代码:obalka.xhtml(主要文件)

Code: obalka.xhtml (main document)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Obalka</title>
  </head>
  <body>
    <div id="auto_filled_commands_container">
MAIN div id="auto_filled_commands_container"<br/>
        <iframe id="auto_filled_commands_iframe" src='dopis.xhtml' style="width:98%;height:98%;"/>
    </div>
<div>
<textarea id="OMGWTF" style="width:700px;height:200px">
mdiv = document.getElementById("auto_filled_commands_container");
ifram = document.getElementById("auto_filled_commands_iframe");
idiv = ifram.contentDocument.getElementById('auto_filled_commands');
OMGWTF = "OMGWTF \n"
+"ok:  mdiv instanceof Element ... "+(mdiv instanceof Element)+" ... "+mdiv+"\n"
+"ok:  mdiv instanceof Object ... "+(mdiv instanceof Object)+" ... "+mdiv+"\n"
+"ko:  idiv instanceof Element ... "+(idiv instanceof Element)+" ... "+idiv+"\n"
+"KO :  idiv instanceof Object ... "+(idiv instanceof Object)+" ... "+idiv+"\n"
;
document.getElementById('result_txta').value = OMGWTF;
</textarea>
<br/><input type="button" value="Eval code in upper textarea (to bypass possible developer tools error)" onclick="
    eval(document.getElementById('OMGWTF').value);
"/><b>(2.)</b><br/>
<textarea id="result_txta" style="width:700px;height:100px">
</textarea>
</div>
  </body>
</html>

代码:Dopis.xhtml(内部框架文档)

Code: Dopis.xhtml (inner frame document)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Dopis</title>
  </head>
  <body>
    <div id="auto_filled_commands">
IFRAME div id="auto_filled_commands"<br/>
        <div id="afc_formular">
IFRAME div id="afc_formular"<br/>
<input id="cmnd" type="text" value="input id='cmnd'" />
<br/><input type="button" value="Click to get browser userAgent" onclick="
    var preEl = this.ownerDocument.getElementById('navUserAgent');
    var cdataEl = preEl.firstChild || preEl;    /* IE don't know CDATA ?! */
    cdataEl.textContent=navigator.userAgent;
"/><b>(1.)</b>
<pre id="navUserAgent"><![CDATA[
]]></pre>
        </div>
    </div>
  </body>
</html>

结果图片(在IE,Chrome,Firefox,Opera中)图片omgwtf.png http://www.sbmintegral.sk/GITHUB/OMGWTF/omgwtf.png

Results image (in IE, Chrome, Firefox, Opera) image omgwtf.png http://www.sbmintegral.sk/GITHUB/OMGWTF/omgwtf.png

推荐答案


主文档和iframe文档有不同的javascript实现???

There are different javascript implementations for main document and for iframe documents ???

是的。 iframe有自己的环境,有自己的全局变量。尝试

Yes. The iframe has its own environment with its own globals. Try

var iwin = ifram.contentWindow;
idiv instanceof iwin.HTMLElement; // true
idiv instanceof iwin.Object; // true

这篇关于IFRAME中的instanceof HTMLElement不是Element或Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆