close
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
span{
color:#a00;
}
</style>
</head>
<body>
<a href="https://api.fnkr.net/testimg/100x100/a00/FFF/?text=A"><img src="https://api.fnkr.net/testimg/100x100/a00/FFF/?text=A"></a>
<a href="https://api.fnkr.net/testimg/100x100/0a0/FFF/?text=B"><img src="https://api.fnkr.net/testimg/100x100/0a0/FFF/?text=B"></a>
<a href="https://api.fnkr.net/testimg/100x100/00a/FFF/?text=C"><img src="https://api.fnkr.net/testimg/100x100/00a/FFF/?text=C"></a>
<h2>cache[<span id="srcforindex"></span>].$img.attr('src')的值是<span id="src"></span></h2>
<script type="text/javascript">
var cache={};
$(document).on('click','a',function(e){
var $img;
var src = this.href;
e.preventDefault();
$img=$('<img/>');
cache[src]={
$img:$img,
isLoading:false
};
/*不用透過cache這個物件去定義,也可以影響到裡面的值*/
$img.attr({
'src':src
});
$('#srcforindex').text(src);
$('#src').text(cache[src].$img.attr('src'));
});
</script>
</body>
</html>
全站熱搜