使jQuery脚本在iframe加载时运行一次
Make jQuery script run once when iframe is loading
我如何使我的jQuery脚本运行一次时,iframe正在加载?这是我的代码。
我的代码
$(document).ready(function(){
//setup a counter to keep our place and cache a selection to the letter wrappers
var counter = 0,
$chars = $(".test").children();
//setup an interval to animate a letter every so often
setInterval(function () {
//select the current letter wrapper and animate it
$chars.eq(counter).effect( "bounce", {times:1}, 500 );
//increment the counter so we animate the next letter next time around
counter++;
//check if the counter still relates to an index of the $chars collection
if (counter >= $chars.length) {
counter = 0;
}
}, 250);
});
看我的小提琴
我需要它运行脚本一次,当我的iframe与id playing_song
正在加载。它可以在任何点。
我已经试过了
我试过使用.ready
,但显然它只会显示它,当它准备好了,而不是当它加载。
我想做的是做一个div的动画,在静态的div。但它不能动态地改变,因为iframe可以在任何时候加载。这个方法只有在父元素被刷新时才有效。
我在找什么
所有我想要的代码是运行一次,当iframe playing_song
正在加载,这可以发生在任何时间,而且不止一次。
您应该能够使用普通javascript完成此操作。考虑这个html:
<div class="test">
<span>M</span>
<span>u</span>
<span>s</span>
<span>i</span>
<span>f</span>
<span>y</span>
</div>
<iframe id="foo" href="https://youtube.com" width="300" height="250"></iframe>
当'#foo'开始加载时,你可以使用这个javascript做一些事情:
var frame_to_load = document.getElementById("foo");
frame_to_load.onload = function() {
// your js here
}
或使用jquery:
$('#foo').on('load', function() {
// your js here
});
如果你可以访问你的iframe,你也可以从你的iframe中运行一个$(document).load(function(){...});
中的js。
在iframe中使用$(document).load
访问load事件
相关文章:
- 使用每500ms运行一次的jquery函数是个好主意吗
- For循环在Jquery中只运行一次
- 谷歌图表在运行应用程序时只能显示一次
- 使函数只运行一次(Javascript)
- 更改路由后,运行一次js脚本
- 只运行一次提取的脚本
- 可能只在for循环内部运行一次代码块
- 在单击事件中,第一次单击时激发代码一次,其余代码在所有单击时运行
- 在Flask中使用.getJSON请求时,只运行一次命令
- 运行“;非“;单击另一个函数中的函数仅一次
- JavaScript只有在最后一次被调用时才运行函数
- 数据互绑定问题:转换器只运行一次,无法绑定元素的 ID
- 您可以在节点.js上一次运行多个应用程序吗?
- 如何一次运行一次循环javascript
- 从命令行一次运行大量摩卡测试的好方法是什么
- 如何让 JavaScript 一次运行一个字符串
- 无法在一次运行中删除多个数组元素
- 如何使jquery函数一次运行一个
- 在一次运行中分配并返回一个变量
- 一次运行2个动画