我在调用自定义插件时遇到问题..插件enlargeImage根本没有从imageSwap jQuery代码中调用。我是新手,还在学习js。在这一点上,我不确定我做错了什么。任何帮助或指导表示赞赏:)
插件代码
(function($){
$.fn.enlargeImage = function() {
return this.each(function() {
alert("This is working!");
// preload images
$("#image_list a").(function() {
var swappedImage = new Image();
swappedImage.src = $(this).attr("href");
});
// set up event handlers for links
$("#image_list").find("a").click(function(evt) {
// swap image
var imageURL = $(this).attr("href");
$("#image").attr("src", imageURL);
//swap caption
var caption = $(this).attr("title");
$("#caption").text(caption);
// cancel the default action of the link
evt.preventDefault(); // jQuery method that's cross-browser compatible
}); // end click
// move focus to first thumbnail
$("li:first-child a:first-child").focus();
});
}
})(jQuery);
jqUERY CODE
$(document).ready(function() {
$('#image_list').enlargeImage();
}); // end ready
HTML
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Swap</title>
<link rel="stylesheet" href="main.css" />
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="jquery.imageSwap.js"></script>
<script src="image_swap.js"></script>
</head>
<body>
<section>
<h1>Ram Tap Combined Test</h1>
<ul id="image_list">
<li><a href="images/h1.jpg" title="James Allison: 1-1">
<img src="thumbnails/t1.jpg" alt=""></a></li>
<li><a href="images/h2.jpg" title="James Allison: 1-2">
<img src="thumbnails/t2.jpg" alt=""></a></li>
<li><a href="images/h3.jpg" title="James Allison: 1-3">
<img src="thumbnails/t3.jpg" alt=""></a></li>
<li><a href="images/h4.jpg" title="James Allison: 1-4">
<img src="thumbnails/t4.jpg" alt=""></a></li>
<li><a href="images/h5.jpg" title="James Allison: 1-5">
<img src="thumbnails/t5.jpg" alt=""></a></li>
<li><a href="images/h6.jpg" title="James Allison: 1-6">
<img src="thumbnails/t6.jpg" alt=""></a></li>
</ul>
<h2 id="caption">James Allison: 1-1</h2>
<p><img src="images/h1.jpg" alt="" id="image"></p>
</section>
</body>
答案 0 :(得分:0)
尝试替换此代码
$("#image_list a").(function() {
var swappedImage = new Image();
swappedImage.src = $(this).attr("href");
});
到
$("#image_list a").each(function() {
var swappedImage = new Image();
swappedImage.src = $(this).attr("href");
});