克隆每个,追加到每个

问题描述 投票:2回答:2

问题:如何从列表中克隆每个图像并将其“粘贴”到另一个列表中?

我已经搜索过,但是找不到足够接近我解决方案的解决方案。

简短摘要:我正在创建一个带有缩略图的幻灯片(jQuery库:Slippry Slider)。我希望缩略图能够动态工作,具体取决于幻灯片的数量。

所以首先我有一些包含幻灯片的HTML:

<ul id="thumbnails">
<li>
    <a href="#slide1">
        <img src="img/image-1.jpg" alt="This is caption 1">
    </a>
</li>
<li>
    <a href="#slide2">
        <img src="img/image-2.jpg"  alt="This is caption 2">
    </a>
</li>
<li>
    <a href="#slide3">
        <img src="img/image-4.jpg" alt="And this is some very long caption for slide 4.">
    </a>
</li>

现在我想为缩略图创建环绕元素,首先是div,然后是ul-元素:

// Create thumb-wrapping-elements
$('.demo_wrapper').append('<div class="thumb-box"></div>');
$('.thumb-box').append('<ul class="thumbs"></ul>');

而且我想检查上面的HTML中有多少张幻灯片:

// How many thumbs? Check how many slides there is.
var count = $("#thumbnails li").length;

创建正确数量的锂元素并设置正确的宽度和值:

// Loop through and create li-elements and links
for (var thumbcounter = 0; thumbcounter < count; thumbcounter++) {

// Whats the width of each thumb?
var thumbwidth = (100/count) + '%';

thumburl = thumbcounter + 1;
$('.thumbs').append('<li><a href="#' + thumburl + '" data-slide="' + thumburl + '" style="width:' + thumbwidth + ';">

这里,我想遍历上面ul#thumbnails li中的每张幻灯片img,并在此处打印。我该怎么办? :)

</a></li>');
};

在此处查看所有代码:http://jborg.se/dev/slippry/demo/test.html

现在,每个a元素内都包含一个“ a”,只是为了使其可见。

编辑:

拇指的宽度更有效,感谢下面的回答。

这是我希望通过上面的jQuery循环输出的内容(目前,我拥有所有正确的功能,除了图像-我不知道如何从HTML复制并在此处以每个缩略图的形式打印):

<div class="thumb-box">
            <ul class="thumbs">
                <li>
                    <a href="#1" data-slide="1">
                        <img src="img/image-1.jpg" alt="This is caption 1">
                    </a>
                </li>
                <li>
                    <a href="#2" data-slide="2">
                        <img src="img/image-2.jpg"  alt="This is caption 2">
                    </a>
                </li>
                <li>
                    <a href="#4" data-slide="3">
                        <img src="img/image-4.jpg" alt="And this is some very long caption for slide 4.">
                    </a>
                </li>
            </ul>
        </div>
javascript jquery html
2个回答
1
投票

这样的东西?

$(document).ready(function() {
    var html = '<div class="thumb-box"><ul class="thumbs"></ul></div>';
    $('.demo').append(html);    

    var count = $("#thumbnails li").length;
    var width = (100.00/count) + '%';   
    var counter = 0;
    $('#thumbnails li').each(function() {
        counter++;
        var newItem = '<li>';
        newItem += '<a href= "' + $(this).find('a').attr('href') + '" ';
        newItem += 'data-slide="' + counter + '">';
        newItem += '<img src="' + $(this).find('img').attr('src') + '" alt="' + $(this).find('img').attr('alt') + '"/>';
        newItem += '</a></li>';
            
        $('ul.thumbs').append(newItem);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
ORIGINAL<br/>
<ul id="thumbnails">
    <li>
        <a href="#1" data-slide="1">
            <img src="img/image-1.jpg" alt="This is caption 1"/>
        </a>
    </li>
    <li>
        <a href="#2" data-slide="2">
            <img src="img/image-2.jpg"  alt="This is caption 2"/>
        </a>
    </li>
    <li>
        <a href="#4" data-slide="3">
            <img src="img/image-4.jpg" alt="And this is some very long caption for slide 4."/>
        </a>
    </li>
</ul>
<br/>
DEMO
<div class="demo">
</div>

1
投票

尝试

// exported and edited the thumbwidth var for efficiency
var thumbwidth = (100/count) + '%';

// Loop through and create li-elements and links
for (var x = 0; x < count; x++) {    
    var imgEl = $('#thumbnails').find('img').eq(x).prop('outerHTML');
    $('.thumbs').append('<li><a href=\"#' + (x+1) + '\" data-slide=\"' + (x+1) + '\" style=\"width:' + thumbwidth + ';\">'+ imgEl + '</a></li>');   
}

PS-抱歉,不提供任何信息和混乱,我在iPad上。

© www.soinside.com 2019 - 2024. All rights reserved.