2016-10-22 71 views
-1
<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <img id="image" src="penguins.jpg"/> 
    <script type="text/javascript" src="js/jquery.js"></script> 
    <script type="text/javascript" src="js/load.js"></script> 
</body> 
</html> 

load.js 这是行不通的

$('#image').load(function(){ 
    alert('Ready'); 
}); 

load.js 这工作

$(document).ready(function(){ 
    alert('Ready'); 
}); 

我想首先加载图像,然后警告框弹出。

+1

这不是['load'(http://api.jquery.com/load/)方法做什么? – Li357

回答

0

将您的<script type="text/javascript" src="js/jquery.js"></script>标签关于<img>标签。因为在图片加载时,jQuery将不会被加载。

您需要有一个timeout才能实现此目的,因为alert会冻结网页的活动并希望用户关注该对话框。所以,它在调用时不会渲染图像。要解决这个问题,请在超时后调用警报。

$('#image').load(function() { 
 
    setTimeout(function() { 
 
    alert('Ready'); 
 
    }, 100) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title></title> 
 
</head> 
 

 
<body> 
 
    <img id="image" src="http://unsplash.it/200/300/" /> 
 
</body> 
 

 
</html>