在 ajax 中生成的锚链接不起作用


Generated anchor links in ajax not working

问题:我有一个通过阿贾克斯填充的。此表中创建了一些本地定位点。当单击锚点时,它应该将隐藏的锚点变为可见并自动滚动到它。当我手动填充时,所有这些都在工作(可见性+滚动),但当通过 Ajax 填充时,则完全不起作用。

我的索引.php文件中有以下结构:

<section id="section1">
   <table></table>
</section>
<section id="section2>
   (this section is hidden via CSS)
</section>
<!-- When the link "More infos" is clicked -->
<script>
    $('.moreInfos').click(function() {
        if ($('#section2').is(':hidden')) {
            $('#section2').slideDown('slow');
        }
    });
</script>
<!-- Ajax call -->
<script language="JavaScript">
        function createInstance()
        {
            var req = null;
            if (window.XMLHttpRequest)
            {
                req = new XMLHttpRequest();
            } 
            else if (window.ActiveXObject) 
            {
                try {
                    req = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e)
                {
                    try {
                        req = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) 
                    {
                        alert("XHR not created");
                    }
                }
                }
            return req;
        };
        function storing(data)
        {
            var element = document.getElementById('banques');
            element.innerHTML = data;
        }
        function submitForm()
        { 
            var req =  createInstance();
            var montant = document.getElementById("montant").value;
            var mois = document.getElementById("selectMois").value;
            var taux = '<?php echo $taux; ?>' ;
            var data = "taux=" + taux +  "&montant=" + montant+  "&mois=" + mois+"&tag=" + 1;
            req.onreadystatechange = function()
            { 
                if(req.readyState == 4)
                {
                    if(req.status == 200)
                    {
                        storing(req.responseText);  
                    }   
                    else    
                    {
                        alert("Error: returned status code " + req.status + " " + req.statusText);
                    }   
                } 
            }; 
            req.open("POST", "fonction/table.php", true); 
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.send(data); 
        } 
</script>

"模拟"链接调用 ajax 中的 php 文件,该文件将加载表。这是在 Ajax 中调用的 php 文件:

<?php
    include('BDD.php');
    echo'   <tr>
                <th></th>
                <th>Banque</th>
                <th>Taux</th>
                <th>Frais</th>
                <th>Mensualité</th>
                <th>Plus d''infos</th>
            </tr>';
    $tag=1;
    $sql="select * from banque where BAN_change=0 and BAN_tag=".$_POST['tag']." ORDER BY BAN_Ordre";
    $select=$bdd->query($sql);
    $result=$select->fetchAll();
    $nb=count($result);
    if ($nb!=0){
        foreach($result as $value){
            $taux=$_POST['taux']+$value['BAN_Taux_Credit'];
            $mensu=$_POST['montant']/$_POST['mois'];
            $mensu+=$mensu*$taux/100;
            echo'<tr>';
                echo'<td><img width="50" height="20" src="img/'.$value['BAN_Id'].'/img.jpg" /></td>';
                echo'<td>'.$value['BAN_Nom'].'</td>';
                echo'<td>'.$taux.'</td>';
                echo'<td>'.$value['BAN_Frais'].'</td>';
                echo'<td>'.$mensu.'</td>';
                echo('<td><a href="#section2" class="moreInfos">More infos</a></td>');
            echo'</tr>';
        }
    }
?>

摘要:当用户单击"更多信息"时,应该会出现 #section2,浏览器窗口会滚动到它。现在,当我用手填充时,这工作得很好。然后显示 #section2,浏览器滚动到 #section2。当我通过 Ajax 执行此操作时,锚点不再工作。

谢谢

因为当您添加新事件时,事件不会神奇地附加

$('.moreInfos').click(function() {
    if ($('#section2').is(':hidden')) {
        $('#section2').slideDown('slow');
    }
});

代码需要使用事件委托

$(document).on("click", '.moreInfos', function() {
    if ($('#section2').is(':hidden')) {
        $('#section2').slideDown('slow');
    }
});

这可能是由于 HTML 未加载到 DOM 中。请尝试使用:

$(document).on('click', '.selector',  function() {
      alert("Working");
    });

"事件处理程序仅绑定到当前选定的元素;在代码调用 .on() 时,它们必须存在于页面上。若要确保元素存在且可以选择,请在文档就绪处理程序中对页面上 HTML 标记中的元素执行事件绑定。如果要将新 HTML 注入到页面中,请在将新 HTML 放入页面后选择元素并附加事件处理程序。

如果这有效,那么您可以在之后对其进行微调。

问候