0

我有一个小问题。因此,我使用 Ajax 将另一个页面的内容加载<div><select>. 但问题是,我只想<div>将该外部页面中的特定内容加载到我的主页中。这是主页的代码:

<script type="text/javascript">
$(function(){
    createListeSelectWithDefault("categorie", <?php echo getJsCategorieListe()?>);
});
function showListes(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","liste_result?q="+str,true);
xmlhttp.send();
}
</script>
<div id="content">
    <div id="bloc">
        <div id="title">Combiner</div>
        <div class="mx">
            <form name="new_combiner">
                <select id="categorie" name="categorie" onchange="showListes(this.value)"></select>
            </form>
        </div>  
        <div class="mx">
            <div id="txtHint">

            </div>
        </div>
    </div>
</div>

这是我的外部页面:

<html>
    <div> some other kind of text, no need to post it here, but can't take it away</div>
<div id="mainDiv">
    <?php
    $q=$_GET["q"];
    $liste = getListeByCategorie($q);
    echo "<table border='1'>
    <tr>
    <th>Titre</th>
    <th>Vues</th>
    </tr>";

    foreach($liste as $row)
    {
        echo "<tr>";
        echo "<td>" . $row->titre() . "</td>";
        echo "<td>" . $row->vue() . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    ?>
</div>
           <div> some other kind of text, no need to post it here, but can't take it away</div>

所以你去,仅此而已。我该怎么办?我只想<div id="mainDiv">将 加载到我的页面中。

4

1 回答 1

1

请更彻底地研究 API。这个特定的用例已经介绍过了。

看这里:http
://api.jquery.com/load/ (参见页面上标题为“加载页面片段”的部分)

从 API 页面:

$('#result').load('ajax/test.html #container');

这将将具有 id 容器的 div 从外部页面 test.html 加载到请求页面上具有 id 结果的 div 中。没有比这更简单的了(根据您的要求更改外部文档选择器)。

于 2013-03-08T18:13:57.840 回答