Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

javascript - Can a button click cause an event in iframe?

Let's say I have a button right next to an Iframe and in that Iframe is a download link. Is it possible that when I click the outside button, the download link on the iframe will be clicked?

http://jsfiddle.net/idude/9RQ3N/

<button type="button">Click Me!</button>

<iframe src="http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download" height="400" width="800">

For example, in the jsfiddle link above, how could I make it so that the w3 picture would be downloaded by just clicking the button?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...