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

时间:2015-02-25 11:10:46

标签: javascript jquery load

我希望在完成加载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命令,所以文件准备就绪无法帮助我。

它的工作方式与文档不同。我希望在加载Image之后启动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">

4 个答案:

答案 0 :(得分:0)

div元素不会引发加载事件。

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

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

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

答案 1 :(得分:0)

使用此document ready function()

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

答案 2 :(得分:0)

您也可以尝试这种方式来检查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');
  } 
} 

答案 3 :(得分:0)

试试这个

<script type="text/javascript">
                jQuery(document).ready(function($) {

                    $(window).load(function(){
                      alert($("#firstAmazon").attr("class"));
                    });

                });
            </script>