开发者

Jquery Repeating Problem

开发者 https://www.devze.com 2023-03-31 05:28 出处:网络
I would like to trade once a function of the change. but for the change, constantly trying to link the exchange function of detecting a change to the previous transactions. As an example, I put alert.

I would like to trade once a function of the change. but for the change, constantly trying to link the exchange function of detecting a change to the previous transactions. As an example, I put alert. As you can see in the alert process is constantly growing. I just clicked in the process being repeated this process to make what can I do?.

Thank you for your attention.

revision of the code running : http://www.smyrnart.com/deeplinking/index.html

My codes;

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>


I think you should first unbind the previously bound event then bind new event.

Like $.address.unbind('change').change(function(event) {... instead of just $.address.change(function(event) {

Hope this helps you.


The reason why you get more and more alerts every time you click a link in this example, is that every time ajax call is finished with a success, the code in a returned script tag is evaluated hence every time you click a link jQuery binds handler to every link it can find on a page. Since there can be more than one handler for any event in Javascript every time you click on a link in example the handler is attached. So if you click 3 times you get 3 alerts, click 15 times and you get 15 alerts (15 click handlers attached to any link on the page).

0

精彩评论

暂无评论...
验证码 换一张
取 消