问题
I want to load an iframe on click, this is what I have so far:
$("#frame").click(function () {
$('this').load("http://www.google.com/");
});
It doesn't work. This is the complete code: JS Bin
回答1:
$("#button").click(function () {
$("#frame").attr("src", "http://www.example.com/");
});
HTML:
<div id="mydiv">
<iframe id="frame" src="" width="100%" height="300">
</iframe>
</div>
<button id="button">Load</button>
回答2:
Try $(this).load("/file_name.html");. This method targets a local file.
You can also target remote files (on another domain) take a look at: http://en.wikipedia.org/wiki/Same_origin_policy
回答3:
$("#frame").click(function () {
this.src="http://www.google.com/";
});
Sometimes plain JavaScript is even cooler and faster than jQuery ;-)
回答4:
here is Iframe in view:
<iframe class="img-responsive" id="ifmReport" width="1090" height="1200" >
</iframe>
Load it in script:
$('#ifmReport').attr('src', '/ReportViewer/ReportViewer.aspx');
回答5:
Just in case anyone still stumbles upon this old question:
The code was theoretically almost correct in a sense, the problem was the use of $('this') instead of $(this), therefore telling jQuery to look for a tag.
$(document).ready(function(){
$("#frame").click(function () {
$(this).load("http://www.google.com/");
});
});
The script itself woudln't work as it is right now though because the load() function itself is an AJAX function, and google does not seem to specifically allow the use of loading this page with AJAX, but this method should be easy to use in order to load pages from your own domain by using relative paths.
来源:https://stackoverflow.com/questions/7177080/how-do-i-load-an-url-in-iframe-with-jquery