Try (this pattern)
$(function () {
var url = ["http://example.org"]; // `url`, e.g., `http://example.org`
var iframe = $("<iframe>", {
"id": "frame",
"width": "400px",
"height": "300px"
});
$.when($("body").append(iframe))
.then(function (b) {
var img = $("<img>", {
"id" : "frameimg",
"width": "400px",
"height": "300px",
}).css("background", "blue");
var a = $("<a>", {
"href" : url[0],
"html" : img
}).css("textDecoration", "none");
var button = $("<button>", {
"html": "click"
}).css("fontSize", "48px")
.one("click", function (e) {
$(b).find("#frame").contents()
.find("body a")
.get(0).click()
});
$(button).appendTo(b);
$(b).find("#frame").contents()
.find("body").html(a);
});
});
jsfiddle http://jsfiddle.net/guest271314/2x4xz/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…