Javascript 更改 iframe 的 src,然后重新加载 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8989247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:19:02  来源:igfitidea点击:

change src of iframe and then reload the iframe

javascriptjquery

提问by Jordashiro

I managed to change an iframe's src with javascript

我设法用 javascript 更改了 iframe 的 src

var path='images/tattoo/fullsize/';
var h=$('#borderlessFrame').height();
var bigFrame=$('#borderlessFrame');
function loadGallery(num)
{
    bigFrame=$('#borderlessFrame');
    var galPath=path + num;             // path to the image
    h=$('#borderlessFrame').height();
    var source=bigFrame.attr('src');
    source='i load some address here';
}

But for now i see the old content of the iframe , is there a way to reload the iframe ( only iframe not the whole page) The thing i am trying to achieve is a simple gallery , thumb pictures on the bottom and a large picture on the top ( iframe ). On click on any of the thumbs , i change the content of the iframe without reloading the actual page.

但是现在我看到了 iframe 的旧内容,有没有办法重新加载 iframe(只有 iframe 而不是整个页面)我想要实现的是一个简单的画廊,底部的拇指图片和大图片顶部( iframe )。单击任何拇指时,我会更改 iframe 的内容,而无需重新加载实际页面。

Keep in mind that i am new to html/javascript/jquery. So basically i need a way(function?) to reload the iframe.

请记住,我是 html/javascript/jquery 的新手。所以基本上我需要一种方法(函数?)来重新加载 iframe。

采纳答案by Sarim

To set attribute, you need to call .attr( attributeName, value )

要设置属性,您需要调用 .attr( attributeName, value )

Try this:

尝试这个:

var source='i load some address here';
bigFrame.attr('src', source);

Reference:

参考:

jQuery .attr()

jQuery .attr()

回答by Rich O'Kelly

jQuery has the loadmethod, which is useable like so (assuming borderlessFrameis the id of the <iframe>):

jQuery有所述load方法,这是可用的,像这样(假设borderlessFrame是的的ID <iframe>):

var iFrame = $('#borderlessFrame');
iFrame.load('http://theurltoload.com')

However, iframes seem like an unnecessary approach for your requirements, consider looking into CSS and the jQuery DOM manipulation methods a little more.

但是,对于您的需求而言,iframe 似乎是一种不必要的方法,请考虑多研究一下 CSS 和 jQuery DOM 操作方法。