使用 jquery 查找所有 iframe srcs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10277907/
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
finding all iframe srcs with jquery
提问by Martin
is there a way, with jquery, to for exapmle on click, find all iframe elements, remove their src= tag, and give it back? sort of, refresh :)
有没有办法,使用 jquery,例如单击时,找到所有 iframe 元素,删除它们的 src= 标记,然后将其返回?有点,刷新:)
Was looking for foreach function or something like that, but I'M rather hopeless at this :(
正在寻找 foreach 函数或类似的东西,但我对此很无望:(
Thanks for your time, Mart
谢谢你的时间,玛特
回答by coolguy
$(document).ready(function() {
$('#somebuttonid').click(function() {
$("iframe").each(function() {
var src = $(this).attr('src');
$(this).attr('src', src);
});
});
});
回答by Starx
You can refresh all the iframe like this
您可以像这样刷新所有 iframe
$("iframe").each(function() {
$(this).attr('src', $(this).attr('src'));
});
回答by Felix Kling
You can pass a function to .attr()
[docs](or .prop()
[docs]):
您可以将函数传递给.attr()
[docs](或.prop()
[docs]):
$('iframe').attr('src', function(index, val) {
return val;
});
This function is executed for each element. It's a bit more concise than using an explicit .each
loop.
该函数针对每个元素执行。它比使用显式.each
循环更简洁一些。
回答by Andreas Wong
You don't even need jQuery:
你甚至不需要 jQuery:
for(var i = 0; i < frames.length; i++) {
frames[i].src = frames[i].src;
}