【问题标题】:Grabbing all <a> tags in a specific div and displaying them抓取特定 div 中的所有 <a> 标签并显示它们
【发布时间】:2012-09-14 21:23:20
【问题描述】:

所以我的投资组合网站有一个小问题(您可以在 here 看到它)

当您点击作品集时,顶部会打开以显示详细信息(标题、年份、角色、描述)以及照片。我已经能够让每个项目替换所有文本数据,但我似乎无法让图像加载到缩略图中。

我已经能够在网站上的所有图像中获得我正在寻找的最后一张图像,但没有显示每个项目的每张照片。

这是我正在使用的 HTML:

<section id="details">
    <div class="pagewrapper">
        <section id="main-img">
            <article id="big-img">
                <img src="" alt="big-img" />
            </article>



            <article class="small-img-container">
                <a href="#"><img src="#" alt="smallimg" class="small-img" /></a>
            </article>
        </section>

        <section id="description">
            <h3></h3>
            <h4></h4>
            <h5></h5>
            <p></p>
        </section>
    </div>

    <div class="clear"></div>

    </section>



    <section id="portfolio">
        <div class="pagewrapper">
            <h2 class="sectionTitle">Portfolio</h2>

            <div class="thumb">
                <a class="small" href="#" title="David Lockwood Music" data-year="2010" data-role="Sole Wordpress Developer" data-description="David Lockwood is a musician and an educator based in New Hampshire who came to me needing a website for his musical career. I fully developed his site using Wordpress as a CMS, creating a custom template based on the design by Jeremiah Louf. Jeremiah and I worked together on the website's UX design."><img src="images_original/davidcover.png" alt="thumb" />

                    <div class="hide">                      
                        <a href="images/davidlockwood/homepage.png" ></a>
                        <a href="images/davidlockwood/blog.png"></a>
                        <a href="images/davidlockwood/shows.png"></a>
                        <a href="images/davidlockwood/bio.png"></a>
                        <a href="images/davidlockwood/photos.png"></a>

                    </div>

                    <h3>David Lockwood Music</h3>
                    <div class="clear"></div>
                </a>
            </div><!--thumb-->

这里是 jQuery:

$(document).ready(function(){

    var proj = {};

    $('.thumb a').click(function() {
        $('#details').slideDown(1000);



        $('.hide a').each(function() {
            proj.img = $(this).attr("href");
            $('.small-img-container img').attr('src',proj.img);

        });

        alert("the image is " + proj.img);//is it getting the image URLS?
        proj.title = $(this).attr("title");
        proj.year = $(this).attr("data-year");
        proj.role = $(this).attr("data-role");
        proj.description = $(this).attr("data-description");



        $('#description h3').text(proj.title);
        $('#description h4').text(proj.year);
        $('#description h5').text(proj.role);
        $('#description p').text(proj.description);

    });

});

有人知道我如何只抓取特定项目的图像,将它们全部显示为缩略图,然后使这些缩略图可点击以查看更大的图像吗?

谢谢!

【问题讨论】:

标签: jquery html image attr


【解决方案1】:
$('.hide a').each(function() {
    proj.img = $(this).attr("href");
    $('.small-img-container img').attr('src',proj.img);

});

proj.img 将始终获取最后一张图片的值,因为您正在遍历所有 a 标签

如果我理解正确,这就是你需要的:

$('.thumb a').click(function() {

    var imageSrc = $(this).find('img[alt="thumb"]').attr('src');

    $('#big-img > img').attr({ src: imageSrc });

    var $thumbs = $(this).next('div.hide').find('img');

    $.each($thumbs, function(i, img){

        var $thumb = $(img)
            .click(function(){
                 $('#big-img > img').attr({ src: $(img).attr('src') });
             })
             .appendTo('#small-img-container');            


    });

});

【讨论】:

  • 所以现在我将它们放在了数组中,并且我可以看到它们带有警报,我如何将它们从数组中取出并放入缩略图列表中?我试过用 for 循环遍历数组,但它似乎仍然不起作用。变量我; for(i = 0;i
  • 现在我真的很困惑。我不完全确定新代码在这里做了什么。我看到它正在找到一个 img 标签,然后将该 img 标签的 src 更改为 a 标签中的 href,然后将其附加到正文(或者在我的情况下为“.small-img-container img”)。这听起来不错,但它仍然无法正常工作。当我在 if 语句中发出警报时,它永远不会出现在页面上。有没有更好的方法来完成整个过程?
  • @TaylorSwyter 它没有找到 img 标签。它为每个在 div 标记中具有 href 的 a-tag 生成一个新的 img 标记,并且在单击的图像旁边具有隐藏类。如果您仍然没有得到您需要的东西,请详细说明并尽力提供帮助
  • 所以基本上,如果您查看页面,我有一个项目列表,该列表由项目图像和 a-tag 中的标题组成。每个项目都有几张我想为他们展示的图片。单击项目时,详细信息部分会向下滑动并显示项目的详细信息以及带有缩略图的小型照片库。我的文字工作正常,现在只是照片的问题。一旦我得到了显示的照片列表,我只需要它们中的每一个在点击时显示一个更大的图像。我发誓我已经很接近解决这个问题了。
  • @TaylorSwyter 大图是指灯箱还是类似的东西?
【解决方案2】:

所以,我重写了所有内容,但我认为这会更容易。

使用数据属性并从那里构建所有内容。我的解决方案要求您使用点击后完整显示的缩小图像(通过 CSS)。

Fiddle.

jQuery:

$("div#links > a").click(function(e) {
    var ID = $(this).data("content");
    var price = $(this).data("price");
    var descr = $(this).data("description");
    var rating = $(this).data("rating");

    $("div#images > img").hide();
    $("span#price, span#description, span#rating").text("").hide();

    $("div#images").children("img#" + ID).fadeToggle("slow");
    $("div#text span#price").text(price);
    $("div#text span#description").text(descr);
    $("div#text span#rating").text(rating);
    $("span#price, span#description, span#rating").fadeIn("fast");
    $("div#text").fadeIn("fast");

    e.preventDefault();
});

$("div#images > img").click(function() {
    $(this).toggleClass("active");
    var marginL = -$(this).width() / 2;
    $("div#images > img.active").css("margin-left", marginL);
    $("div#images > img:not(.active)").css("margin-left", "0px");

    if ($("div#overlay").is(":hidden")) {
        $("div#overlay").fadeIn("fast");
    }
    else {
        $("div#overlay").fadeOut("fast");
    }
});​

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多