She*_*byZ 2 javascript jquery html5 dom
所以我在Html5演示中弄乱了Html5 PostMessage示例,我创建了一个示例jsfiddle,看看我是否理解它是如何协同工作的.
该演示利用了document.getElementById(...),我认为可以用jQuery选择来代替$("#..."),但我被困在因为从jQuery选择返回的对象不具有访问contentWindow而document.getElementById(...)做.
document.getElementById("frame1").contentWindow.postMessage("Hello from another domain", "http://dl.dropbox.com"); // works
$("#frame1").contentWindow.postMessage("Hello from another domain", "http://dl.dropbox.com"); // no dice
Run Code Online (Sandbox Code Playgroud)
我并不完全精通jQuery,知道从选择器调用结果对象的许多方法中的哪一个回到我将看到的结果document.getElementById(...).
$("#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.
Run Code Online (Sandbox Code Playgroud)
完整代码:
$("#frame1")[0].contentWindow.postMessage("Hello from another domain", "http://dl.dropbox.com"); // DICE!
Run Code Online (Sandbox Code Playgroud)
但我发现使用jQuery进行选择id然后从中提取DOM元素并且根本不使用jQuery是很尴尬的.怎么了document.getElementById?那15个额外的字符?