gpt4 book ai didi

javascript - 图库功能似乎被调用了两次

转载 作者:可可西里 更新时间:2023-11-01 15:01:20 25 4
gpt4 key购买 nike

我有一个图片库,其中有一张主图片和下面的 5 个缩略图,当您单击缩略图时,图片会发生变化,这工作正常,但我还在两侧放置了两个箭头以滚动浏览图片它有点管用,但它每隔一秒就会跳过一张图片。我尝试放入警报以查看发生了什么,它们被触发了两次。

我的HTML如下:

<tr id="product_main_image">
<td colspan="5">
<img src="product_images/catalog.png" id="1">
<span id="image_left" style="bottom: 233px;"><img src="web/left_arrow.png"></span>
<span id="image_right" style="bottom: 233px;"><img src="web/right_arrow.png"></span>
</td>
</tr>

<tr id="product_thumbnail">
<td><img src="product_images/catalog.png" id="1"></td>
<td><img src="product_images/config.png" id="2"></td>
<td><img src="product_images/customers.png" id="3"></td>
<td><img src="product_images/marketing.png" id="4"></td>
<td><img src="product_images/sales.png" id="5"></td>
</tr>

我的JS如下:

$("#product_thumbnail img").on({
mouseover: function(){
$(this).css({'cursor': 'pointer'});
},
mouseout: function(){
$(this).css({'cursor': 'default'});
},
click: function(){
var imageURL = $(this).attr('src');
var imageID = $(this).attr('id');
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('id', imageID);
}
});

$("#image_left").on({
mouseover: function(){
$(this).css({'cursor': 'pointer'});
},
mouseout: function(){
$(this).css({'cursor': 'default'});
},
click: function(){
var curImageID = $("#product_main_image img").attr('id');
curImageID--;
var imageURL = $("#"+curImageID).attr('src');
alert (imageURL);
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('id', curImageID);
}
});

$("#image_right").on({
mouseover: function(){
$(this).css({'cursor': 'pointer'});
},
mouseout: function(){
$(this).css({'cursor': 'default'});
},
click: function(){
var curImageID = $("#product_main_image img").attr('id');
curImageID++;
var imageURL = $("#"+curImageID).attr('src');
alert (imageURL);
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('id', curImageID);
}
});

编辑

我的 HTML 是如何从 php 生成的

$sql = mysqli_query($con, "SELECT * FROM product_images WHERE product='$product_id'");
$imageCount = mysqli_num_rows($sql);
if ($imageCount > 0) {
$i = 0;
while($row = mysqli_fetch_array($sql)){
$image = $row["image"];
$i++;
$gallery .= "<td><img src='product_images/$image' data-id='$i'></td>";
}
}
$sql = mysqli_query($con, "SELECT * FROM product_images WHERE product='$product_id' LIMIT 1");
$imageCount = mysqli_num_rows($sql);
if ($imageCount > 0) {
while($row = mysqli_fetch_array($sql)){
$first_image = $row['image'];
$main_image .= "<img src='product_images/$first_image' data-id='1'>";
}
}

我的实际 HTML

<table id="gallery">
<tr id="product_main_image">
<td colspan='5'>
<?php echo $main_image; ?>
<span id="image_left"><img src="web/left_arrow.png"></span>
<span id="image_right"><img src="web/right_arrow.png"></span>
</td>
</tr>
<tr id="product_thumbnail">
<?php echo $gallery; ?>
</tr>
</table>

最佳答案

您使用的 id 不是唯一的。他们应该是。

而是使用 data-id

解决方法:

$("#product_thumbnail img").on({
click: function() {
var imageURL = $(this).attr('src');
var imageID = $(this).attr('data-id');
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('data-id', imageID);
}
});

$("#image_left").on({
click: function() {
var curImageID = $("#product_main_image img").attr('data-id');
curImageID--;
if( curImageID > 0) {
var imageURL = $("#product_thumbnail img[data-id=" + curImageID + "]").attr('src');
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('data-id', curImageID);
}
}
});

$("#image_right").on({
click: function() {
var curImageID = $("#product_main_image img").attr('data-id');
curImageID++;
if( curImageID <= $('#product_thumbnail img').length) {
var imageURL = $("#product_thumbnail img[data-id=" + curImageID + "]").attr('src');
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('data-id', curImageID);
}
}
});
#product_thumbnail img:hover,
#image_left,
#image_right{
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="product_main_image">
<td colspan="5">
<img src="http://via.placeholder.com/100x100" data-id="1"><br />
<span id="image_left" style="bottom: 233px;">
<!--img src="web/left_arrow.png"-->
left
</span>
<span id="image_right" style="bottom: 233px;">
<!--img src="web/right_arrow.png"-->
right
</span>
</td>
</tr>

<tr id="product_thumbnail">
<td><img src="http://via.placeholder.com/100x100" data-id="1"></td>
<td><img src="http://via.placeholder.com/110x100" data-id="2"></td>
<td><img src="http://via.placeholder.com/120x100" data-id="3"></td>
<td><img src="http://via.placeholder.com/130x100" data-id="4"></td>
<td><img src="http://via.placeholder.com/140x100" data-id="5"></td>
</tr>
</table>

关于javascript - 图库功能似乎被调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49033585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com