警告变量时双重弹出不同的值?

时间:2015-01-30 17:53:36

标签: javascript jquery iframe

我正在尝试获取变量的值,我正在使用警报进行检查。

HTML:

<div class="item">
   <span class="video" data-vimeoid="http://vimeo.com/XXX"></span>
</div>
<div class="item">
   <span class="video" data-vimeoid="http://vimeo.com/YYY"></span>
</div>
<div class="item">
   <span class="video" data-vimeoid="http://vimeo.com/ZZZ"></span>
</div>

的jQuery

$(document).on( 'click', '.item:not(.is-expanded)', function() {
  var url = $(this).find(".video").data("vimeoid");
  alert(url);
});

第一次触发http://vimeo.com/XXX然后点击popUp上的确定后,另一个警告显示undefined

完整的代码在这里:

$(document).on( 'click', '.item:not(.is-expanded)', function() {
    $(".item").removeClass('is-expanded');
    $(".caption").css("display", "block");
    $(".wrapVideo").css("display", "none");
    $("img").fadeIn();
    $(this).addClass('is-expanded');
    $(".is-expanded .caption").removeClass("loaded-caption").css("display", "none");
    $(".is-expanded img").css("display", "none");
    $(".is-expanded .wrapVideo").css("display", "block");
    $("#list").isotope("layout");
    var url = $(this).find(".video").data("vimeoid");
    alert(url);
    var tokens = url.split("/");
    var id = tokens[3];
    var videoSpan =  $(this).find("span.video");
    var iframe = $('<iframe/>', {
            'frameborder' : 0,
            'class' : 'embed-responsive-item',
            'src' : 'http://player.vimeo.com/video/'+ id + '?api=1&player_id=player webkitAllowFullScreen  mozallowfullscreen allowFullScreen'
    });
    videoSpan.replaceWith(iframe);
});

1 个答案:

答案 0 :(得分:0)

这是我解决它的方式:

$(document).on( 'click', '.item:not(.is-expanded)', function() {
    $(".item").removeClass('is-expanded');
    $(this).addClass('is-expanded');
    $(".caption").css("display", "block");
    $(".item:not(.is-expanded) img").fadeIn();
    $(".item:not(.is-expanded) .wrapVideo").css("display", "none");
    var videoSpan =  $(this).find("span.video");
    var url = videoSpan.data("vimeoid");
    if(url){
        var tokens = url.split("/");
        var id = tokens[3];
        var iframe = $('<iframe/>', {
                'frameborder' : 0,
                'class' : 'embed-responsive-item',
                'src' : 'http://player.vimeo.com/video/'+ id + '?api=1&player_id=player webkitAllowFullScreen  mozallowfullscreen allowFullScreen'
        });
        videoSpan.replaceWith(iframe);
    } 
    $(".is-expanded .caption").removeClass("loaded-caption").css("display", "none");
    $(".is-expanded img").css("display", "none");
    $(".is-expanded .wrapVideo").css("display", "block");
    $("#list").isotope("layout");
});