簡體   English   中英

只選擇一個 <div> 從外部頁面使用Ajax / PHP

[英]Only select a <div> from an external page with Ajax/PHP

我有一個小問題。 因此,在<select>檢測到更改之后,我正在使用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">加載到我的頁面中。

請更徹底地研究API。 這個特殊的用例已經涵蓋了。

在這里看: http : //api.jquery.com/load/
(請參見頁面上標題為“加載頁面片段”的部分)

從API頁面:

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

這將加載在div id為容器,從外部頁的test.html,與請求頁面上的ID結果股利。 沒有比這更簡單的了(根據您的要求更改外部文檔選擇器)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM