English 中文(简体)
重新装入后单击事件失败
原标题:onclick event fails after reloading

我想实现的是:

I m having a div which keeps track of stats of users. When clicked on a user, the contents of the div are replaced by details of a users by an ajax request echoed page. Every 10 seconds, the div will refresh using ajax and therefore the old contents, the stats of ALL users are displayed again.

My problem: Upon loading the page, the onclick event works great, when clicked on a user; the details will load. After about 10 seconds, the div contents are refreshed by the old ALL user stats.

目前为止还不错, 除了点击事件对新内容无效...

这是我的代码:

标注描述 :

var x;
var namen;
window.onload = function(){
    x = true;
    $("submitnieuw").observe( click , addturf);
    $("submitdelete").observe( click , verwijderturf);
    setInterval(function (){
        if(x === true){
            alert("a");
            x = $( stats ).getElementsByTagName( tr );
            jQuery("#stats").load("stats.php");
            jQuery("#recent").load("vandaag.php");
            namen = $( stats ).getElementsByTagName( tr );
            for(var i = 1; i < namen.length; i++){  
                namen[i].observe( click , select);
            }   
        }
    }, 10000);
    namen = $( stats ).getElementsByTagName( tr );
    for(var i = 1; i < namen.length; i++){  
        namen[i].observe( click , select);
    }   
};

function select(naam){
    //highlight the selected list element
    var name = naam.findElement( tr ).id;
    jQuery.ajax( details.php ,{
        data: {
             Naam : name,
             door : $("door2").value
        },
        type:  post , 
        success: function(data){
            $("stats").innerHTML = data;
        },
        error: ajaxFailure
    });
}

function detail(ajax){
    var text = ajax.responseText;
    alert(text);
    L = text;
}

function verwijderturf() {
    var naam = $("naam").value;
    $("naamnieuw").value = "";
    $("naam").value = "";
    $("redennieuw").value = "";
    jQuery.ajax( server.php ,{
        data: {
             mode :  verwijderturf ,
             naam : naam,
             door : $("door2").value
        },
        type:  post , 
        success: update,
        error: ajaxFailure
    });
}


function addturf() {
    var naam = $("naamnieuw").value;
    var reden = $("redennieuw").value;
    $("naamnieuw").value = "";
    $("naam").value = "";
    $("redennieuw").value = "";
    jQuery.ajax( server.php ,{
        data: {
             mode :  addturf ,
             naam : naam,
             door : $("door2").value,
             reden : reden
        },
        type:  post , 
        success: update,
        error: ajaxFailure
    });
}

function update(ajax){
    jQuery("#stats").load("stats.php");
    jQuery("#recent").load("vandaag.php");
}


function ajaxFailure(ajax, exception) {
    alert("Error making Ajax request:" + 
        "

Server status:
" + ajax.status + " " + ajax.statusText + 
        "

Server response text:
" + ajax.responseText);
    if (exception) {
        throw exception;
    }
}

stats.php (再次装入的内容) :

<?php
    include_once("db.php"); 
?>


<?php
    $names = mysql_query("SELECT Naam From users");
    $feedData =   ;
    $feedData .= "<fieldset>";
    $feedData .= "<legend>Turfjesteller</legend>";
    $feedData .= "<table border=0>";
    $feedData .= "<tr>";
    $feedData .= "<td>Naam</td>";
    $feedData .= "<td>Aantal</td>";
    $feedData .= "<td>Gedaan</td>";
    $feedData .= "<td>Resterend</td>";
    $feedData .= "</tr>";
    while($r = mysql_fetch_array($names)){
        $feedData .= "<tr id=".$r[ Naam ].">";
        $feedData .="<td>" . $r[ Naam ] . "</td>";

        $sql="SELECT * FROM turfjes WHERE Naam= ".$r[ Naam ]."  AND Type= Adtje ";
        $result=mysql_query($sql);
        $count=mysql_num_rows($result); //count = adtjes
        $sql2="SELECT * FROM turfjes WHERE Naam= ".$r[ Naam ]."  AND Type= Turfje ";
        $result2=mysql_query($sql2);
        $count2=mysql_num_rows($result2); //count2 = turfje     

        $feedData .="<td>" . $count2 . "</td>";
        $feedData .="<td>" . $count . "</td>";
        $feedData .="<td>" . ($count2-$count) ."</td>";     
        $feedData .="</tr>";
    } 
    $feedData .="</table>";
    $feedData .="</fieldset>";
    echo($feedData);
?>

如前所述,页面看起来完全一样,但点击事件不再有效。

迪夫的内容完全一样 所以我不知道什么是错的

namen = $( stats ).getElementsByTagName( tr );
for(var i = 1; i < namen.length; i++){  
     namen[i].observe( click , select);
}

应该获得所有表格行, 并再次单击事件...

在10秒后用 stats.php 替换div 数据后, 点击事件不再有效 。

有人能帮我吗?

最佳回答

您可以使用 jQuery s on () 方法设置初始点击处理器 :

$(document).on( click ,  <div selector> , select);

这将确保点击事件仍然记录在新元素上。

问题回答

每次ajax 响应后, 尝试单击想要的 div 时重新绑定 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签