我被困data()
在我创建的 iFrame 上。
这就是我正在做的事情:
// options > my configuration object
// options.src = eg "path/foo.html"
// options.id = uuid
// options.parent = $(elem)
// options.param = JSON object
newHTML = document.createElement("iframe");
newHTML.setAttribute("src", options.src);
newHTML.setAttribute("frameborder", 0);
newHTML.setAttribute("seamless", "seamless");
newHTML.setAttribute("data-id", options.id);
newParentElement = options.parent.parent()[0];
options.parent.replaceWith( newHTML );
// select
newRootElement = newParentElement.querySelectorAll(
'[data-id="'+options.id+'"]'
);
// add configuration
$( newRootElement[0] ).load(function () {
var newElement = $(this);
if (options.param) {
newElement.contents().find("body").attr("foo","bar").data("config", options.param);
}
});
当我查看我的 iframe 和它的 body 标签时,它attr("foo")
已正确设置,我也可以像这样对其进行控制台:
console.log(newElement.contents().find("body").attr("foo"));
但是当我尝试使用or来控制config
使用时,就像这样:data()
data("config")
console.log(newElement.contents().find("body").data("config"));
它总是返回undefined
问题:
为什么不能data()
在 iFrame 上设置 jQuery?还是我做错了什么?
感谢您的帮助!