gpt4 book ai didi

php - foreach 循环和 jQuery

转载 作者:行者123 更新时间:2023-12-01 08:05:42 27 4
gpt4 key购买 nike

我有一个 PHP 和一个 jQuery 脚本,用来显示一些图像。左侧有一张大图像,右侧有 4 个缩略图。每次用户单击图像缩略图时,它都会显示在左侧的大图像占位符上。

这是我用来显示大图像和缩略图的 PHP 代码:

<div class="pic"><img title="<?php echo $current->alttext ?>" alt="<?php echo $current->alttext ?>" src="<?php echo $current->imageURL; ?>" />
</div>
<ul class="ngg-gallery-list-ir">
<!-- Thumbnail list -->
<?php foreach ( $images as $image ) : ?>
<?php if ( $image->hidden ) continue; ?>
<li id="ngg-image-<?php echo $image->pid ?>" class="ngg-thumbnail-list <?php if ($image->pid == $current->pid) echo 'selected' ?>" >
<a href="<?php echo $image->imageURL ?>" title="<?php echo $image->description ?>" >
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
</li>
<?php endforeach; ?>

这是我用来在用户单击任何缩略图时更新大图像的 jQuery:

jQuery(document).ready(function($){

// handle the click of thumbnail images
// redirect it to change the main image
$(".ngg-thumbnail-list a").click(function(){
var src = $(this).attr("href");
$(".ngg-galleryoverview-ir .pic img").attr("src", src);
return false;
});

// preload the large images
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
});
}
// populate the list of images to load
preload(images);
});

在此设置中一切正常,但我还需要在主大图像下方显示其标题和描述。这是我正在使用的代码:

<div class="container-title-description">
<div class="title"><?php echo $current->alttext ?></div>
<div class="descripton"><?php echo $current->caption ?></div>
</div>

问题是这样的:如果我在 foreach 循环中添加此代码,我会在每个缩略图下方得到标题和描述。如果我在主图像更改时在 foreach 循环之外添加此代码,标题和描述将保持不变。我该如何解决这个问题?

您可以在 website 上查看此设置的外观。 .

最佳答案

您已经将标题和描述添加为 anchor 元素内的隐藏 title 属性,因此只需提取它们并根据需要更新 HTML:

$(".ngg-thumbnail-list a").click(function(){
var src = $(this).attr("href"),
desc = $(this).attr('title'),
title = $(this).find('img').attr('title');
$(".ngg-galleryoverview-ir .pic img").attr("src", src);
$('.container-title-description .title').text(title);
$('.container-title-description .description').text(desc);
return false;
});

初始 HTML(在 foreach 循环之外):

<div class="container-title-description">
<p class="title"></p>
<p class="description"></p>
</div>

关于php - foreach 循环和 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16861555/

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