2013-01-24 39 views
0

我正在构建在线URL拍卖工具。以下是用户输入购买网址后的结果页面。当这个结果页面被调用2个其他结果页面正在创建,但他们需要一段时间,所以我想要做的是设置菜单栏阅读两个按钮上的“加载”和href到#。然后,我使用setInterval来每2秒轮询一次,检查是否创建了第二个文件。一旦成功,回调会将按钮重新加载到活动的hrefs并标记它们['查找Whois'和'评估']而不是“加载”。Ajax之后重新绑定菜单Js

问题:我该如何重新绑定菜单栏js以便在ajax调用之后工作?香港专业教育学院被SO阅读这个确切的主题,但在使用.live().on()display(),并且more失败...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
    <head> 
     <title>Domain Auctions R US</title> 
     <link rel="stylesheet" href="../css/style.css" type="text/css" media="screen"/> 
     <style> 
      body{ 
       background-repeat:no-repeat; 
       background-color:#fff; 
      } 

      span.reference{ 
       position:fixed; 
       left:10px; 
       bottom:10px; 
       font-size:12px; 
      } 
      span.reference a{ 
       color:#aaa; 
       text-transform:uppercase; 
       text-decoration:none; 
       text-shadow:1px 1px 1px #000; 
       margin-right:30px; 
      } 
      span.reference a:hover{ 
       color:#ddd; 
      } 
      ul.sdt_menu{ 
       margin-left:auto; 
       margin-right:auto; 
       margin-top:0px; 
      } 

     </style> 
    </head> 
<body> 
<br/><br/><br/><br/> 
     <div class="content" id="content"> 
      <ul id="sdt_menu" class="sdt_menu"> 
       <li> 
        <a href="#"><!-- When the other two results pages are ready, this needs to be a real link --> 
         <img src="../images/googleMap.jpg" alt=""/> 
         <span class="sdt_active"></span> 
         <span class="sdt_wrap"> 
          <span class="sdt_link">Loading</span> 
          <span class="sdt_descr">Page not yet available</span> 
         </span> 
        </a> 
       </li> 
       <li> 
        <a href="worth.html"> 
         <img src="../images/pawnShop.jpg" alt=""/> 
         <span class="sdt_active"></span> 
         <span class="sdt_wrap"> 
          <span class="sdt_link">Loading</span> 
          <span class="sdt_descr">Page not yet available</span> 
         </span> 
        </a> 
       </li> 
      </ul> 
     </div> 

     <h1>The URL you are searching for is currenly TAKEN.</h1> 

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script> 
     <script type="text/javascript" src="../js/jquery.easing.1.3.js"></script> 
     <script type="text/javascript"> 
      $(function() { 
       $('#sdt_menu > li').bind('mouseenter',function(){ //LINE SWITCH 1 
        var $elem = $(this); 
        $elem.find('img') 
         .stop(true) 
         .animate({ 
          'width':'170px', 
          'height':'170px', 
          'left':'0px' 
         },400,'easeOutBack') 
         .andSelf() 
         .find('.sdt_wrap') 
         .stop(true) 
         .animate({'top':'140px'},500,'easeOutBack') 
         .andSelf() 
         .find('.sdt_active') 
         .stop(true) 
         .animate({'height':'170px'},300,function(){ 
         var $sub_menu = $elem.find('.sdt_box'); 
         if($sub_menu.length){ 
          var left = '170px'; 
          if($elem.parent().children().length == $elem.index()+1) 
           left = '-170px'; 
          $sub_menu.show().animate({'left':left},200); 
         } 
        }); 
       }).bind('mouseleave',function(){ //LINE SWITCH 2 
        var $elem = $(this); 
        var $sub_menu = $elem.find('.sdt_box'); 
        if($sub_menu.length) 
         $sub_menu.hide().css('left','0px'); 

        $elem.find('.sdt_active') 
         .stop(true) 
         .animate({'height':'0px'},300) 
         .andSelf().find('img') 
         .stop(true) 
         .animate({ 
          'width':'0px', 
          'height':'0px', 
          'left':'85px'},400) 
         .andSelf() 
         .find('.sdt_wrap') 
         .stop(true) 
         .animate({'top':'25px'},500); 
       }); 
      }); 

       var myVar=setInterval(function(){ajax_request()},2000); 
        function ajax_request() { 
        $.ajax({ 
        type: "GET", 
        url: "http://exampleServer/urlID/4b49d2/index.html", 
        dataType: "script", 
        success: function() { 
        $('#conn').load("ajax-loader.html"); 


        clearInterval(myVar); 
        }, 
        error: function() { 
        alert("error"); 
        } 
       }); 
       } 
     </script> 

</body> 
</html> 

Ajax的loader.html

<ul id="sdt_menu" class="sdt_menu"> 
       <li> 
        <a href="../map.html"> 
         <img src="../images/googleMap.jpg" alt=""/> 
         <span class="sdt_active"></span> 
         <span class="sdt_wrap"> 
          <span class="sdt_link">Find out Whois</span> 
          <span class="sdt_descr">Map of Who Owns Domain</span> 
         </span> 
        </a> 
       </li> 
       <li> 
        <a href="worth.html"> 
         <img src="../images/pawnShop.jpg" alt=""/> 
         <span class="sdt_active"></span> 
         <span class="sdt_wrap"> 
          <span class="sdt_link">Appraisal</span> 
          <span class="sdt_descr">Real Time Value of Domain</span> 
         </span> 
        </a> 
       </li> 
      </ul> 

回答

1

这应该是足够简单,采用.on()代替.bind()

$('#content') 
    .on('mouseenter', '.sdt_menu > li', function() { 
    }) 
    .on('mouseleave', '.sdt_menu > li', function() { 
    }) 

注意.bind().on()工作不同,一个简单的重命名不起作用。

+0

谢谢你为你的帖子。我试过了,除了菜单栏动画在ajax之后没有响应之外,它工作正常。具体来说,我评论了'LINE SWITCH 1和2',其中'开''绑定'。或者那不是我应该做的? – Chris

+0

@Chris是的,这就是你应该改变的地方......如果动画没有响应,你怎么知道它的工作原理,因为几乎所有的代码都是关于动画的? –

+2

你是不是只为'on'切换'bind'呢?参数不同,需要匹配@Jack发布的内容,以及更改父级jQuery选择器。 – jmoerdyk