[英]Execute .js immediately after particular <div> has been loaded
$(window).load(function ())
works very reliably, but you have to wait till all the page has been loaded to see the change on the div ( although it's at the top of the page). $(window).load(function ())
工作非常可靠,但是您必须等待所有页面加载完毕才能看到div上的更改(尽管它位于页面顶部)。
Simply putting code after div doesn't work after refresh and the same with .ready function. 只是将代码放在div之后,刷新后不起作用,并且与.ready函数相同。
Example: JS Fiddle demo 示例: JS Fiddle演示
I'm not sure that you can use jQuery's load
method on an element like <div>
because it doesn't fire a load event. 我不确定您是否可以在
<div>
类的元素上使用jQuery的load
方法,因为它不会触发load事件。 But images do: 但是图像可以:
Perhaps wrap it in jQuery's ready
method: 也许将其包装在jQuery的
ready
方法中:
$(window).ready(function(){
$('div img').load(function(){
// code to run after div's image has loaded
});
})
EDIT : 编辑 :
In response to your fiddle, if you're wanting to run JavaScript before the <p>
then you can just put the <script>
before it in the document. 为了回应您的小提琴,如果您想在
<p>
之前运行JavaScript,则只需将<script>
放在文档中即可。 JavaScript gets run as it is encountered by the browser, so this should work: 当浏览器遇到JavaScript时,它就会运行,因此应该可以:
<div id="texture">
<!-- All your spans -->
</div>
<script>
alert('this will run before your p element is loaded');
</script>
<p>I would prefer to run script before this text is loaded</p>
我建议您使用css隐藏文本,并在jQuery初始化后使用jQuery显示文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.