Javascript jQuery 强制为 iframe 设置 src 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7551912/
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 02:41:43  来源:igfitidea点击:

jQuery Force set src attribute for iframe

javascriptjquerydom

提问by testndtv

I have a main page (actually a JSP) with an iframe inside it as;

我有一个主页(实际上是一个 JSP),里面有一个 iframe;

<iframe name="abc_frame" id="abc_frame" src="about:blank" frameborder="0" scrolling="no"></iframe>

Now there are multiple links on the main page (rendered dynamically via JSP) which can try to set the src to some URL...

现在主页上有多个链接(通过 JSP 动态呈现)可以尝试将 src 设置为某个 URL...

Now my question is,using jQuery (may be live()), can I override that in such a way that the "src" attribute for abc_frame would always have some particular value (e.g. "somefixedURL")

现在我的问题是,使用 jQuery(可能是 live()),我可以覆盖它,使 abc_frame 的“src”属性始终具有某些特定值(例如“somefixedURL”)

I cannot capture the link clicking, as it is completely dynamically created via some Java code and hence I am trying to override the iframe src ?

我无法捕获链接点击,因为它是通过一些 Java 代码完全动态创建的,因此我试图覆盖 iframe src ?

回答by PiTheNumber

Use attr

使用属性

$('#abc_frame').attr('src', url)

This way you can get and set every HTML tag attribute. Note that there is also .prop(). See .prop() vs .attr()about the differences. Short version: .attr()is used for attributes as they are written in HTML source code and .prop()is for all that JavaScript attached to the DOM element.

通过这种方式,您可以获取和设置每个 HTML 标记属性。请注意,还有.prop(). 有关差异,请参阅.prop() 与 .attr()。简短版本:.attr()用于属性,因为它们是用 HTML 源代码编写的,并且.prop()用于附加到 DOM 元素的所有 JavaScript。

回答by kalyfe

if you are using jQuery 1.6 and up, you want to use .prop()rather than .attr():

如果您使用 jQuery 1.6 及更高版本,您想使用.prop()而不是.attr():

$('#abc_frame').prop('src', url)

See this questionfor an explanation of the differences.

有关差异的解释,请参阅此问题

回答by Brian Rizo

While generating the links set the target to the iframes name property and you probably wont have to deal with jquery at all.

在生成链接时,将目标设置为 iframes 名称属性,您可能根本不必处理 jquery。

<a href="inventory.aspx" target="contentframe"  title="Title Inventory">
<iframe id="iframe1" name="contentframe"  ></iframe>

回答by Jayendra

$(document).ready(function() {
    $('#abc_frame').attr('src',url);
})

回答by Hari

Setting src attribute didn't work for me. The iframe didn't display the url.

设置 src 属性对我不起作用。iframe 没有显示网址。

What worked for me was:

对我有用的是:

window.open(url, "nameof_iframe");

Hope it helps someone.

希望它可以帮助某人。

回答by Arend

<script type="text/javascript">
  document.write(
    "<iframe id='myframe' src='http://www.example.com/" + 
    window.location.search + "' height='100' width='100%' >"
  ) 
</script>  

This code takes the url-parameters (?a=1&b=2) from the page containing the iframe and attaches them to the base url of the iframe. It works for my purposes.

此代码从包含 iframe 的页面中获取 url 参数 (?a=1&b=2),并将它们附加到 iframe 的基本 url。它适用于我的目的。

回答by Jan Pfeifer

You cannot set FIX iframe's src or prevent javascript/form submit to change its location. However you can put script to onloadof the page and change action of each dynamic link.

您不能设置 FIX iframe 的 src 或阻止 javascript/form submit 更改其位置。但是,您可以将脚本加载到页面上并更改每个动态链接的操作。

回答by vernmic

$(".excel").click(function () {
    var t = $(this).closest(".tblGrid").attr("id");
    window.frames["Iframe" + t].document.location.href = pagename + "?tbl=" + t;
});

this is what i use, no jquery needed for this. in this particular scenario for each table i have with an excel export icon this forces the iframe attached to that table to load the same page with a variable in the Query String that the page looks for, and if found response writes out a stream with an excel mimetype and includes the data for that table.

这就是我使用的,不需要 jquery。在这个特定场景中,对于每个带有 excel 导出图标的表,这会强制附加到该表的 iframe 加载具有页面查找的查询字符串中的变量的同一页面,如果找到,响应会写出一个流excel mimetype 并包含该表的数据。