我正在创建iframe插件,以使网络应用中的内容能够嵌入第三方网站。很像FB插件,第三方网站插入一个div,JS函数将一个iframe附加到div。
再次像FB插件一样,我希望能够在父div上定义数据属性,并在iframe中拥有这些控件内容。谷歌搜索后,似乎iframe内容可以访问iframe上的数据属性。例如
# 3rd party site
<script src='my-script.js'></script> #see below
<div id="embedded-div" data-my-attribute="HELLO WORLD"></div>
#within iframe content
alert( $(window).attr('data-my-attribute') );
这是对的吗? 而且(我之前没有回答这个问题的原因),以编程方式将数据属性添加到iframe的正确语法是什么?
我正在创建iframe,如下所示
#my-script.js
window.onload = function() {
var elm = document.getElementById('embedded-div');
var src = "http://location/of/iframe/content";
var iframe = document.createElement('iframe');
iframe.src = src;
# how to define data attribute on iframe?
# I've unsuccessfully tried variations like the following
# iframe.data('my-attribute', $('#embedded-div').data('my-attribute') );
# iframe.data.my-attribute = $('#embedded-div').data('my-attribute') ;
elm.appendChild(iframe);
};
或者我在这里走错了路?