简体   繁体   English

使用Ajax获取DIV内容

[英]Get DIV content using Ajax

I'm able to get the result from the second page, but the thing is that I'm getting everything that is in the HTML output, I only want certain parts of it... 我可以从第二页获得结果,但事实是,我正在获取HTML输出中的所有内容,我只想要其中的某些部分...
I'm already about 1K of code lines using jQuery, and JavaScript, and by loading the whole page from the ajax it just break the whole app... 我已经使用jQuery和JavaScript编写了大约1K的代码行,并且通过从ajax加载整个页面就破坏了整个应用程序...
The content from the secondPage is dynamic and it has javascript and jquery, which I don't need that for the end result but I do to get the process... so.. secondPage中的内容是动态的,并且具有javascript和jquery,对于最终结果,我不需要这些内容,但我确实希望获得该过程。

secondpage.php is just a form base on my first page, so my "Ajax" is in the "main_page.php" like this... secondpage.php只是我首页上的一个表单,因此我的“ Ajax”就在“ main_page.php”中,如下所示:

function getXMLHTTP() { //function to return the xml http object
    var xmlhttp=false;
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {
        try{
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }
    return xmlhttp;
}
function getNenes(medi) {
    var strURL ="secondpage.php?id="+medi;
    var req = getXMLHTTP();
    if (req) {
        req.onreadystatechange = function() {
            if (req.status == 200) {
                document.getElementById('copy').innerHTML=req.responseText;
            }
        }
        req.open("GET", strURL, true);
        req.send(null);
    }
}

this is a functional script if you want to use it for whatever you want... 如果您想将其用于任何用途,这是一个功能脚本...
Now on my secondpage.php this is what I have.... sort of... 现在在我的secondpage.php上,这就是我所拥有的。

<?php
include ('connect.php');
// lets make a header
header('Content-Type: text/html; charset=utf-8');

if (isset($_GET['id'])) {
 // lets make a query and all that is need
 // to fetch the information based on the ID #
} else {
 // lets add a header 
 header ("Location: you_are_not_allow.php");
}
?>
<!-- let put our HTML -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <!-- Required -->

<div id="copy">
 <!-- everything that is here is what we want -->
 <!-- form input's, div's, span's ...etc -->
 <!-- I have a few foreach for input's array -->
 <!-- Anything else is useless for the main page -->
</div>
 <!-- Now I need to put the info for each input that I might get -->
 <!-- for that I'm using array's -->
<?php
echo '<script type="text/javascript">';
foreach ($m as $n) {
echo '$("#d_'.$c.'").children("option[value=\''.$d->ds.'\']").prop("selected",true);';
    $c++;
}
echo '</script>';
?>

So, until this point everything is working "just fine", the thing is that when I get the result from the secondpage.php I don't want the < script > only what is inside the first div with the ID copy... 因此,直到这一步一切都“正常”为止,问题是当我从secondpage.php获得结果时,我不希望<script>仅是具有ID副本的第一个div中的内容。

Things I don't use and cause conflicts on main_page.php are: 我不使用并引起main_page.php冲突的是:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

and

<?php
    echo '<script type="text/javascript">';
    foreach ($m as $n) {
    echo '$("#d_'.$c.'").children("option[value=\''.$d->ds.'\']").prop("selected",true);';
        $c++;
    }
    echo '</script>';
    ?>

So how can I just fetch what is inside my <div id = "copy"> ? 那么,如何获取<div id = "copy">呢?

You can fetch the inside of this tag with JQuery $("#copy") to select the tag. 您可以使用JQuery $("#copy")获取此标签的内部以选择标签。 $("#copy").html() to get what's inside $("#copy").html()获取内部内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM