admin管理员组

文章数量:1417720

So I was messing around with the Html5 PostMessage sample at Html5 Demos and I created a sample jsfiddle to see if I understood how it worked together.

The demo makes use of document.getElementById(...) which I thought could be replaced with the jQuery selector $("#..."), but I got stuck on the because the returned object from the jQuery select does not have access to contentWindow while document.getElementById(...) does.

document.getElementById("frame1").contentWindow.postMessage("Hello from another domain", ""); // works

$("#frame1").contentWindow.postMessage("Hello from another domain", ""); // no dice

I'm not entirely well versed in jQuery to know which of the many methods to call on the results object from the selector to get back to the result I would see from document.getElementById(...).

So I was messing around with the Html5 PostMessage sample at Html5 Demos and I created a sample jsfiddle to see if I understood how it worked together.

The demo makes use of document.getElementById(...) which I thought could be replaced with the jQuery selector $("#..."), but I got stuck on the because the returned object from the jQuery select does not have access to contentWindow while document.getElementById(...) does.

document.getElementById("frame1").contentWindow.postMessage("Hello from another domain", "http://dl.dropbox."); // works

$("#frame1").contentWindow.postMessage("Hello from another domain", "http://dl.dropbox."); // no dice

I'm not entirely well versed in jQuery to know which of the many methods to call on the results object from the selector to get back to the result I would see from document.getElementById(...).

Share Improve this question edited Apr 25, 2012 at 20:39 gdoron 150k59 gold badges302 silver badges371 bronze badges asked Apr 25, 2012 at 20:30 ShelbyZShelbyZ 1,5041 gold badge14 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7
$("#frame1")    // This a jQuery object that encapsulate the DOM element.
$("#frame1")[0] // this is the DOM element.
//Or
$("#frame1").get(0) // this is the DOM element.

Full code:

$("#frame1")[0].contentWindow.postMessage("Hello from another domain", "http://dl.dropbox."); // DICE!

Updated Fiddle

But I find it awkward to use jQuery to select by id then extract the DOM element out of it, and not using jQuery at all. what's wrong with document.getElementById? those 15 extra chars?

本文标签: javascriptHtml5 postMessage using jQuerybut not jQuerypostMessage scriptStack Overflow