2012-01-23 91 views
1

我正在创建一个简单的悬停“地图”。基本上悬停在一个标记和一个弹出的细节揭示。无论如何,除了我想添加一个关闭按钮,它大部分工作正常。现在用户可以在弹出框外点击并关闭它,但是我需要在角落添加一个关闭按钮。但是,我的闭幕式不会开火。目前我只是试图在用户点击内容弹出窗口中的任何位置并关闭时设置它。一旦我有这个工作,我将它应用到容器中的一个样式的div。这里是我的HTML的示例:jquery事件不会触发

<div id="container"> 
<div id="truck"><img src="eVie.png" width="637" height="280" alt="Evie" /></div> 
<div class="marker a"> 
<div class="content a inactive"><img src="solarpanel.jpg" width="240" height="205" alt="Solar Panels" /> 
    <h2>Solar Panels</h2> 
    <p>Here Comes The Sun: These solar panels capture light and heat from the sun to power exhibits when I’m out visiting around town.</p> 
</div> 
</div> 
<div class="marker b"> 
<div class="content b inactive"><img src="tailpipe.jpg" width="240" height="205" alt="Tailpipe Area" /> 
    <h2>Tailpipe Area</h2> 
    <p>No tailpipe, no fumes. That’s how I roll.</p> 
</div> 
</div> 
<div class="marker c"> 
<div class="content c inactive"><img src="plug.jpg" width="240" height="205" alt="Plug" /> 
    <h2>Plug</h2> 
    <p>Not An Ordinary Plug: Big trucks need more energy. To get all the energy I need, I have to have a bigger plug and a special outlet!</p> 
</div> 
</div> 
</div> 

这里是我的jQuery。除了content.active点击功能之外,它都可以正常工作。为什么?我很难过这个!

$(document).ready(function(){ 

    $(".marker").hover(
     function(){ 
      $(this).children(".content.inactive").addClass("show"); 
      $(this).children(".content.inactive").animate({width: 155}, "fast");  
     } 
     , 
     function(){ 
      $(this).children(".content.inactive").animate({width: 5, height: 23}, "fast"); 
      $(this).children(".content.inactive").removeClass("show");  
     } 
    ); 

    $(".content.inactive").click(
     function(){ 
      $(this).removeClass("inactive"); 
      $(this).addClass("active"); 
      $(this).animate({width: 435, height: 205}, "fast"); 
      $(".inactive").addClass("disabled"); 
      $(".disabled").removeClass("inactive"); 
      $(".disabled").parent().addClass("dim"); 
      $("#truck img").addClass("dim"); 
     } 
    ); 

    $(".content.active").click(
     function(){ 
      $(this).removeClass("active"); 
      $(this).removeClass("show"); 
      $(this).addClass("inactive");  
      $(this).animate({width: 5, height: 23}, "fast"); 
      $(".disabled").addClass("inactive"); 
      $(".disabled").parent().removeClass("dim"); 
      $(".inactive").removeClass("disabled"); 
      $("#truck img").removeClass("dim"); 
     } 
    ); 

    $(".content").click(function(){ return false; }); 
    $(document).on("click", function() { 
     $(".content").animate({width: 5, height: 23}, "fast"); 
     $(".disabled").addClass("inactive"); 
     $(".inactive").removeClass("disabled"); 
     $(".inactive").parent().removeClass("dim"); 
     $("#truck img").removeClass("dim"); 
     $(".active").addClass("inactive"); 
     $(".show").removeClass("show"); 
     $(".active").removeClass("active"); 
    }); 
}); 

这里的链接网站:http://stlenergy.org/evie/

任何帮助,将不胜感激。我确定我的逻辑不对,或者我错过了一些显而易见的事情。谢谢!

+0

妮可,我当时不知道,但jQuery有.on()方法,它可以更好地满足您的需求。 – mowwwalker

+0

谢谢,我会考虑将它切换出来。我为最后一个函数使用了.on()方法。我认为这对jQuery(1.7)来说是比较新的。 –

回答

1

使用此:

function inactiveClick(){ 
      $(this).removeClass("inactive"); 
      $(this).addClass("active"); 
      $(this).animate({width: 435, height: 205}, "fast"); 
      $(".inactive").addClass("disabled"); 
      $(".disabled").removeClass("inactive"); 
      $(".disabled").parent().addClass("dim"); 
      $("#truck img").addClass("dim"); 
     $(this).unbind('click'); // added this 
     $(this).click(activeClick); // added this 
     return false; // added this 
} 
function activeClick(){ 
      $(this).removeClass("active"); 
      $(this).removeClass("show"); 
      $(this).addClass("inactive");  
      $(this).animate({width: 5, height: 23}, "fast"); 
      $(".disabled").addClass("inactive"); 
      $(".disabled").parent().removeClass("dim"); 
      $(".inactive").removeClass("disabled"); 
      $("#truck img").removeClass("dim"); 
     $(this).unbind('click'); // added this 
     $(this).click(inactiveClick); // added this 
     return false; // added this 
} 
$(document).ready(function(){ 

    $(".marker").hover(
     function(){ 
      $(this).children(".content.inactive").addClass("show"); 
      $(this).children(".content.inactive").animate({width: 155}, "fast");  
     } 
     , 
     function(){ 
      $(this).children(".content.inactive").animate({width: 5, height: 23}, "fast"); 
      $(this).children(".content.inactive").removeClass("show");  
     } 
    ); 

    $(".content.inactive").click(
     inactiveClick 
    ); 

    $(".content.active").click(
     activeClick 
    ); 
    //$(".content").click(function(){ return false; }); // removed so I can unbind the elements 
    $(document).on("click", function() { 
     $(".content").animate({width: 5, height: 23}, "fast"); 
     $(".disabled").addClass("inactive"); 
     $(".inactive").removeClass("disabled"); 
     $(".inactive").parent().removeClass("dim"); 
     $("#truck img").removeClass("dim"); 
     $(".active").addClass("inactive"); 
     $(".show").removeClass("show"); 
     $(".active").removeClass("active"); 
    }); 
}); 

的问题,如鲨鱼提到的,那是你的元素没有active直到类他们被点击,所以他们永远不会被绑定一个点击事件。

我做了以下内容:

  • 分的无效和有效点击到自己的职能
  • 新增返回false两个功能
  • 没有限制在点击功能结束的元素
  • 将新的点击事件绑定到点击函数末尾的元素

编辑:我不知道它的时候,但jQuery有一个。对()方法,这是完美的您的情况:

$(document).ready(function(){ 

    $(".marker").hover(
     function(){ 
      $(this).children(".content.inactive").addClass("show"); 
      $(this).children(".content.inactive").animate({width: 155}, "fast");  
     } 
     , 
     function(){ 
      $(this).children(".content.inactive").animate({width: 5, height: 23}, "fast"); 
      $(this).children(".content.inactive").removeClass("show");  
     } 
    ); 

    $('body').on('click',".content.inactive",function(){ 
     $(this).removeClass("inactive"); 
      $(this).addClass("active"); 
      $(this).animate({width: 435, height: 205}, "fast"); 
      $(".inactive").addClass("disabled"); 
      $(".disabled").removeClass("inactive"); 
      $(".disabled").parent().addClass("dim"); 
      $("#truck img").addClass("dim"); 
     } 
    ); 

    $('body').on('click',".content.active",function(){ 
     $(this).removeClass("active"); 
      $(this).removeClass("show"); 
      $(this).addClass("inactive");  
      $(this).animate({width: 5, height: 23}, "fast"); 
      $(".disabled").addClass("inactive"); 
      $(".disabled").parent().removeClass("dim"); 
      $(".inactive").removeClass("disabled"); 
      $("#truck img").removeClass("dim"); 
    }); 
    $(".content").click(function(){ return false; }); // removed so I can unbind the elements 
    $(document).on("click", function() { 
     $(".content").animate({width: 5, height: 23}, "fast"); 
     $(".disabled").addClass("inactive"); 
     $(".inactive").removeClass("disabled"); 
     $(".inactive").parent().removeClass("dim"); 
     $("#truck img").removeClass("dim"); 
     $(".active").addClass("inactive"); 
     $(".show").removeClass("show"); 
     $(".active").removeClass("active"); 
    }); 
}); 
+0

真棒!完美的作品。非常感谢。我(显然)仍然在学习很多关于jQuery的知识,我无法理解这一点。我感谢您的帮助!这是有道理的看到它。 –

+0

@NicoleMcCoy,没问题。感谢鲨鱼,他发现了错误! – mowwwalker

1

我相信这是因为当你的听众被宣布的时候,将不会有包含类别contentactive的包装集。因此,click()侦听器不会被分配给任何元素。

您将能够做这样的事情:

$(".content").click( 
function(){ 
    var inactiveObjects = $(this).hasClass("inactive"); 
    inactiveObjects.removeClass("inactive"); 
    inactiveObjects.addClass("active"); 
    inactiveObjects.animate({width: 435, height: 205}, "fast"); 
    $(".inactive").addClass("disabled"); 
    $(".disabled").removeClass("inactive"); 
    $(".disabled").parent().addClass("dim"); 
    $("#truck img").addClass("dim"); 

    var activeObjects = $(this).hasClass("active"); 
    activeObjects.removeClass("active"); 
    activeObjects.removeClass("show"); 
    activeObjects.addClass("inactive");  
    activeObjects.animate({width: 5, height: 23}, "fast"); 
    $(".disabled").addClass("inactive"); 
    $(".disabled").parent().removeClass("dim"); 
    $(".inactive").removeClass("disabled"); 
    $("#truck img").removeClass("dim"); 
} 
); 
+0

我可能误解了你的答案,当弹出窗口显示时,div有两个类的内容和活动。我已经用萤火虫验证了这一点。 –

+1

@NicoleMcCoy正确,但是当你的监听器赋值代码被执行时,没有一个具有'active'类的对象。 – 2012-01-23 03:16:50

+0

好吧,我还没有真正理解,但我试过上面的代码,它阻止了我的弹出窗口打开。:( –

0

我只是看着您的网站。我认为如果您删除.content.active.content的点击处理程序并将return false;添加到.content.inactive处理程序的末尾,那么您可以通过现有代码获得所需内容。点击活动内容时不会返回false,点击事件会触发document处理程序,并且应该关闭打开的对话框。这将会让您将对话框关闭代码全部保存在一个地方。