将活动div的内容复制到另一个div

时间:2013-09-16 11:07:56

标签: javascript jquery

我需要将clicked div的内容复制到另一个div。我有视频列表以及标题和日期。我需要播放视频并将点击的div的内容复制到另一个。

到目前为止,我能够播放视频,但它始终只复制第一个div的内容。我怎么能陷入活跃的div或我点击的一个。

jsFiddle

上的示例

的jQuery

$(document).ready(function () {

    $('.Play-Video').click(function (e) {
        e.preventDefault();
        var URL = $(this).attr('href');
        $('.video-title').text($(this).text());
        var htm = '<iframe width="438" height="250" src="http://www.youtube.com/embed/' + URL + '?wmode=transparent&rel=0&theme=light&color=white&autoplay=0" frameborder="0" allowfullscreen ></iframe>';

        //$('#video_container').html(htm);
        // $('.video-title').html(htm);
            $(".video-title").html($(".title").html());
        return false;
    });
});

2 个答案:

答案 0 :(得分:1)

你应该这样打电话:

$(".video-title").html($(this).children(".title").html());  //for changing title
$(".video-date").html($(this).children(".date").html());    //for changing date

而不仅仅是

$(".video-title").html($(".title").html());  //this will look for all classes wit name ".title" present in the html code

Working Fiddle更新了小提琴

答案 1 :(得分:0)

这很简单......

看看这个http://jsfiddle.net/YtcDJ

<!-- language: lang-js -->
$(document).ready(function () {
    $('.Play-Video').click(function (e) {
        e.preventDefault();
        var URL = $(this).attr('href');
        $('.video-title').text($(this).text());
        var htm = '<iframe width="438" height="250" src="http://www.youtube.com/embed/' + URL + '?wmode=transparent&rel=0&theme=light&color=white&autoplay=0" frameborder="0" allowfullscreen ></iframe>';
        $(".video-title").html($(".title", $(this)).html());
    });
});

我改变了:$(".video-title").html($(".title").html());
进入:$(".video-title").html($(".title", $(this)).html());

有了这个,'title'将从'this'上下文中获取。

祝你好运!

相关问题