【问题标题】:jquery bindings are conflictingjquery 绑定有冲突
【发布时间】:2014-01-11 03:38:33
【问题描述】:

我将如何重写此代码以使绑定不会冲突?我有一个悬停效果的比例,一旦我单击目标图片(以显示 Youtube 视频),它就会停止响应。

  <script type="text/javascript">
//to scale up on hover
$('#videoandmapwrap').on({
    mouseenter: function () {
        current_h = $(this, 'img')[0].height;
        current_w = $(this, 'img')[0].width;
        $(this).stop(true, false).animate({
            width: (current_w * 2.7),
            height: (current_h * 2.7)
        }, 900);
    },
    mouseleave: function () {
        $(this).stop(true, false).animate({
            width: current_w + 'px',
            height: current_h + 'px'
        }, 400);
    }
}, 'img');

//to reveal from behind placeholder picture
   $('#videoandmapwrap').on("click","img",function(event){
     event.preventDefault();
      video = '<iframe class="piccon" width="200" height="200" src="'+ $(this).attr('data-video') +'"></iframe>';
      $(this).replaceWith(video);  
   });

     </script>

【问题讨论】:

  • 我被引导相信使用事件委托重写代码可以解决问题,但事实并非如此!提前谢谢你!

标签: javascript jquery binding


【解决方案1】:

我认为是因为这条线:

$(this).replaceWith(video); 

基本上,您正在用您创建的 iframe 替换 #videoandmapwrap,因此您的原始内容及其附加事件都消失了。尝试在另一个元素中弹出该视频,而不是您点击的那个

【讨论】:

  • 我明白了,一旦我点击,就不再有 img 了——它被 iframe 取代了。我重复了代码并将所有 img 更改为 iframe,它完成了工作!非常感谢 erikrunia!
猜你喜欢
  • 2012-11-22
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多