简体   繁体   English

更改IFrame src属性

[英]Changing IFrame src attribute

I'm working with a few test pages and I'm having trouble loading the pages in to the IFrame with the src attribute using jQuery. 我正在使用一些测试页面,并且我无法使用jQuery将页面加载到带有src属性的IFrame中。 All the pages are local. 所有页面都是本地的。

Here are the scripts I've tried: 这是我尝试过的脚本:

$(document).ready(function () {
    $("#button1").click(function () {
        $("#iframeBox").attr("src", "Default.aspx");
    });
});

$(document).ready(function () {
    $("#button1").click(function () {
        $("#iframeBox").attr("src") = "Defualt.aspx";
    });
});

This is the markup for the IFrame: 这是IFrame的标记:

<iframe id="iframeBox" src="Copy of Default.aspx"></iframe>

I tried to use this code with online pages and it still didn't work. 我尝试将此代码用于在线页面,但仍然无效。

If using IFrames is a bad idea, could you please recommend me a different method I can use. 如果使用IFrame是一个坏主意,你能否请我推荐一种我可以使用的不同方法。 The idea of the site is to show a lot of galleries with subjects that the user can browse and every page should hold a gallery. 该网站的想法是向许多画廊展示用户可以浏览的主题,每个页面都应该有一个画廊。

I'm using the: galleriffic jQuery plugin. 我正在使用: galleriffic jQuery插件。

Changing the attribute won't change the location of the iframe. 更改属性不会更改iframe的位置。

HTML: HTML:

<iframe id="iframeBox" name="iframeBox" src="watever"></iframe>

JavaScript: JavaScript的:

document.iframeBox.location.href = "Defualt.aspx";

But I recommend to use div's for that: 但我建议使用div:

HTML: HTML:

<div id="iframeBox" data-src="watever.aspx"></div>

JavaScript: JavaScript的:

 $("div#iframeBox").css("overflow", "auto"); // or do it in css
 $.get($("div#iframeBox").data("src")).success(function(data) {
     $("div#iframeBox").html(data);
 }).error(function() { $("div#iframeBox").html("Could not load page."); });

You can change the page like this: 您可以像这样更改页面:

 $.get("Default.aspx").success(function(data) {
     $("div#iframeBox").html(data);
 }).error(function() { alert("Error while changing page!"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM