jquery - keypress on iframe document gets whole html as target (not exact node) -


I have an IFrame with designMode = "on" (yes - I know this is a bad thing)

Let me click on it and do the quiking and echo the node name of the target element.

  $ (function () {var editor = $ ("# editor") [0] .contentWindow; var doc = editor.document; editor.document.designMode = "on"; doc .open (); doc.write ('& lt; div id = "dummy"> test ;); doc.close (); // iframe body var $ body = $ ( Find the content (). ('# Dummy'). After removing $ body, parent (); // clear html ('
gt; hello & lt; / div & gt; '); Var report = function (e) {$ ("# result"). Html ($ ("# result"). Html () + "+ + e Target.nodeName.toLowerCase ());} $ body $ (Doc) .keypress (report); / Pre>

When I "Hello" I click on - I get "div" - that's right, but when I press it - I get "html" instead of "div". How to fix it?

I've done a little bit Google. It can help you:

 < Code> var f = document.getElementById ('iframe_id'); Var fwin = f.contentWindow || F.contentDocument; Fwin.document.designMode = 'on'; Var evt_key = function (e) {e = e || Fwin.event; Var range = zero, ret = null; If (fwin.document.selection) {range = fwin.document.selection.createRange (); Ret = range.parentElement (); } And if (fwin.window.getSelection) {var category = fwin.window.getSelection (). GetRangeAt (0); Ret = range.common ESTNT CONTAINER. ParentNode || Fwin.document; } Fwin.parent.do_some_thing (ret); }; If (fwin.document.attachEvent) {fwin.document.attachEvent ('onkeypress', evt_key); } And if (fwin.document.addEventListener) {fwin.document.addEventListener ('keypress', EVT_key, false); }   

All credits for JetKook (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_25589672.html)

Comments