2014-10-08 24 views
6

Cercando determinato DOM Element per semplice controlloinstanceof HTMLElement in IFRAME non è Element o Object?

isElement = SomeThing instanceof Element 

opere in documento principale, ma non su (tutti?) I nodi in iframe.

uscita Esempio (Google Chrome): (MDIV è DIV nel documento principale, idiv è 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] 

Ci sono diverse implementazioni di javascript per il documento principale e per i documenti iframe ???

Per favore spiegami cosa c'è che non va.

Esempio: (http://www.sbmintegral.sk/GITHUB/OMGWTF/obalka.xhtml)

Codice: obalka.xhtml (documento principale)

<?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> 

Codice: Dopis.xhtml (documento collarino) immagine

<?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> 

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

+0

Che cos'è "Elemento"? in DOM livello 1, la più basilare delle specifiche DOM dal 1998, gli elementi HTML implementano l'interfaccia 'HTMLElement'; non c'è, né è mai esistita, un'interfaccia ufficiale di 'Element'. Detto ciò, perché mai usi XHTML? Ci siamo liberati di quel tempo fa. –

+0

L'elemento è costruttore di HTMLElement! – supipd

+0

L'elemento è costruttore di HTMLElement! L'oggetto è costruttore di qualsiasi oggetto, ecco perché tutti gli oggetti devono essere instanceof Object. Perché XHTML? ... perché HTML non ha CDATA XHTML:

HTML:
 1 < 3 < 134
cosa è più pulito? Se l'HTML è per i popoli pigri che non sono in grado di scrivere correttamente il codice XML, XHTML offre (come XML) molti vantaggi come per esempio. XML islands – supipd

risposta

7

Esistono diverse implementazioni javascript per il documento principale e per i documenti iframe ???

Sì. L'iframe ha il proprio ambiente con i suoi globali. Prova

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

Oh, sembra chiaro, ma perché Firefox ha detto idiv instanceof Element ... true idiv instanceof Object ... false – supipd

+0

Nessuna idea. Forse 'Element == iwin.Element'? – Bergi