2011-08-29 65 views
0

我想换一次函数的变化。但是对于这种变化,不断尝试将检测变化的交换功能与以前的事务联系起来。作为一个例子,我把戒备。正如你在警报过程中看到的一样,这个过程不断增长。我只是点击过程中重复这个过程,使我能做什么?jquery重复问题

感谢您的关注。

版本的代码运行:http://www.smyrnart.com/deeplinking/index.html

我的代码;

的Index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html><head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<script type="text/javascript" src="http://www.asual.com/jquery/address/samples/express/jquery.address-1.3.min.js"></script> 
<script type="text/javascript"> 
     $('a').address(function() {return $(this).attr('href').replace(/^#/, '');}); 
     $.address.change(function(event) { 

      var url=""; 
      if  (event.value=='/index1') {url = "index1.html"} 
      else if (event.value=='/index2') {url = "index2.html"} 

      if (url!==""){ 
       $.ajax({url : url,success : function(data){$("#content").html(data);}}); 
      } 
     }); 
</script> 

</head> 

<body> 
<a title="" href="#/index1/">index1</a> | <a title="" href="#/index2/">index2</a> 
<div id="content" style="margin-top:50px;"></div> 
</body> 

</html> 

Index1.html

<strong>in order;</strong><br/><br/> 

index1<br/> 
index2<br/> 
index1<br/> 
index2<br/><br/> 

alert: problem repeat 

Index2.html

<script type="text/javascript"> 
$.address.bind('change', function(e) { 
//I used as examples only. anything else to do here, but should be resolved in this process of repetition. 
    alert('problem') 

}); 
</script> 

<div> 
$.address.bind('change', function(e) { 
    alert('problem') 
}); 
<strong>Go now #index1, at this #index2</strong> 
</div> 

回答

0

我觉得你应该先解除绑定以前绑定的事件然后绑定新的事件。

$.address.unbind('change').change(function(event) {... ,而不是仅仅$.address.change(function(event) {

希望这有助于你。

+0

感谢user909159。但没有解决我的问题。 –

0

您每次单击this示例中的链接时都会收到越来越多的警报,原因是每次ajax调用成功完成时,就会评估返回的脚本标记中的代码,因此每次单击链接jQuery将处理程序绑定到它可以在页面上找到的每个链接。 由于每当您单击处理程序附加示例中的链接时,可能会有多个处理程序用于Javascript中的任何事件。所以如果你点击3次,你会得到3个警报,点击15次,你会得到15个警报(15个点击处理程序附加到页面上的任何链接)。

+0

我改变http://www.smyrnart.com/deeplinking/index.html也许你现在可以看到问题。 –

+0

我认为这仍然是同样的问题。 **每次**我点击链接“index2”你加载一些脚本和HTML。您必须意识到,任何加载和评估的脚本对整个页面中的DOM(HTML)都有影响(在这种情况下,导致alert()被触发X次,其中X是您点击“index2.html”链接的次数)。也许你应该退出这个“alert”的例子,并给我们举例说明你的代码是真的**的意思。 – WTK