【发布时间】:2015-02-25 11:10:45
【问题描述】:
我想在加载完一个 Div 后执行一个命令。应该很容易,但我无法处理。
//With window it works fine
$(window).load(function()
{
alert($("#firstAmazon").attr("class"));
});
//With an other Object not
$("#firstAmazon").load(function()
{
alert("firstAmazon loaded");
});
<div class="socialIcon" id="firstAmazon">
<a href="amazon" title="Amazon">
<img src="../img/socialBar/amazon.png">
</a>
</div>
那条评论很有帮助
div 元素不会引发加载事件,只有文档、窗口和 img 元素可以。 – Rory McCrossan 16 分钟前
实际上我想在加载 img (amazon.png) 后执行命令,所以文档准备好帮不了我。
它不像文档那样工作。我希望加载图像后触发 load()。
$("#firstPic").load(function()
{
alert("firstPic loaded");
});
<div class="socialIcon">
<img id="firstPic" src="http://www.smartturtle.com/img/header.jpg">
</div>
这里是文档http://api.jquery.com/load-event/
$( "#book" ).load(function() {
// Handler for .load() called.
});
<img src="book.png" alt="Book" id="book">
【问题讨论】:
-
div元素不会引发load事件,只有document、window和img元素会。 -
使用 jquery 的 Document.ready 事件。只有当 div 得到渲染时才会触发 ready 事件。
标签: javascript jquery load