0

我正在尝试在 jQuery 中实现幻灯片放映,其中所有图像及其描述都在带有 ID 的隐藏元素中contain

在这里,我想获取所有包含元素 ( img span) 对,以便对它们进行迭代。但问题是:

var slides = $('#contain').children(); //[object Object]
console.log(slides);
//console.log(slides.get(0)); //< this is also not working

这正在返回,[object Object]但是我希望元素与 DOM 元素一样,因此我可以选择详细信息、迭代它们等。

任何人都可以帮助我如何得到它?

Complete code:

function startSlideShow(interval) {
var slides = $('#contain').children();
console.log("0: " + slides);
for (var i = 0; i < slides.length; i++) {
    setTimeout(
        function () {
            var slide = $(slides[i]).children();
            $('#currentImage').attr('src', slide[0].src).fadeIn(interval * 100);
            $('#slideDesc').html(slide[1].innerHTML).fadeIn(interval * 100);
        }, interval * 1000);
    }
}

in the html:

<article id="contain">
    <div class="image">
        <img src="http://i.imgur.com/925p6M5.jpg" />
        <span>1Lorem Ipsum is simply dummy text of the printing and typesetting industry. </span>
    </div>
    <div class="image">
        <img src="http://i.imgur.com/dbBu5rk.jpg" />
        <span>2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. </span>
    </div>
    <div class="image">
        <img src="http://i.imgur.com/VFxPGEi.gif" />
        <span>3 Lorem Ipsum is simply dummy text of the printing and typesetting industry. </span>
    </div>
</article>

问题在于它给我带来了如下错误: Uncaught TypeError: Cannot read property 'src' of undefined

4

2 回答 2

2

您可以使用 $.each

slides.each(function(){
    console.log($(this));
});
于 2013-09-03T20:58:27.430 回答
1

你可以试试这个

var slides = $('#contain').children().get();
console.log(slides); // array of dom elements
console.log(slides[0]); // first dom element

或者

$('#contain').children().each(function(k, v){
    console.log(v);
});

例子。

于 2013-09-03T21:13:43.957 回答