.load()适用于window,而不是ID

.load() works with window, not with ID

本文关键字:ID window 适用于 load      更新时间:2024-05-31

我想在加载完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)后执行teh命令,所以文档就绪对我没有帮助。

它的工作原理与文档不同。我希望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元素不会引发加载事件。

使用document.ready处理程序,如图所示:-

$(function(){
   alert("firstAmazon loaded");
});

document.ready处理程序在DOM完全加载之后执行。

使用此文档就绪功能()

$(document).ready(function(){
   alert("firstAmazon loaded");
});

试试这个

<script type="text/javascript">
                jQuery(document).ready(function($) {
                    $(window).load(function(){
                      alert($("#firstAmazon").attr("class"));
                    });
                });
            </script>

您也可以尝试这种方法来检查div加载的

var interval;
window.onload = createInterval(); 
function createInterval(){ 
   interval=setInterval(readyCheck(),100); 
} 
function readyCheck(){ 
  if($('#firstAmazon img').get(0).complete){ 
     clearInterval(interval); 
     alert('firstAmazon loaded'); 
  } 
  else { 
     alert('firstAmazon loading');
  } 
}