如何使用jQuery从src获取iFrame id

时间:2022-11-20 20:37:06

What I want to do: I've got an HTML page with some iFrames in it. I need to refresh one particular iFrame after I click on a button.

我想做什么:我有一个包含一些iFrame的HTML页面。我点击一个按钮后需要刷新一个特定的iFrame。

My problem: that iFrame I want to refresh, got a dynamic id, so I can't do this with

我的问题:iFrame我想刷新,得到一个动态id,所以我不能这样做

document.getElementById(FrameID).contentDocument.location.reload(true);

I would like to obtain that iFrame id from his src, because I know a particular string that is contained in it.

我想从他的src中获取iFrame id,因为我知道其中包含的特定字符串。

Question: is it possible to do something like this? And how?

问题:可以做这样的事吗?如何?

2 个解决方案

#1


2  

You can do:

你可以做:

$("iframe[src*=sample]").attr("id");

where "sample" is the string which you are looking at.

其中“sample”是您正在查看的字符串。

Once you have the concerned iframe, you don't need the ID. You can simply reload it using the same selector.

拥有相关的iframe后,您不需要ID。您只需使用相同的选择器重新加载它。

More on the attribute selectors: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

有关属性选择器的更多信息:https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

#2


0  

You can give name attribute to element and then take the id of that object.

您可以为element赋予name属性,然后获取该对象的id。

var name = document.getElementsByName("box");
console.log(name[0].id);

Fiddle code-http://jsfiddle.net/16xhq5ym/2/

#1


2  

You can do:

你可以做:

$("iframe[src*=sample]").attr("id");

where "sample" is the string which you are looking at.

其中“sample”是您正在查看的字符串。

Once you have the concerned iframe, you don't need the ID. You can simply reload it using the same selector.

拥有相关的iframe后,您不需要ID。您只需使用相同的选择器重新加载它。

More on the attribute selectors: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

有关属性选择器的更多信息:https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

#2


0  

You can give name attribute to element and then take the id of that object.

您可以为element赋予name属性,然后获取该对象的id。

var name = document.getElementsByName("box");
console.log(name[0].id);

Fiddle code-http://jsfiddle.net/16xhq5ym/2/