jQueryAJAX将数据重新加载到DOM中,或者使用新数据刷新DOM

jQuery AJAX reloading data into a DOM or refreshing the DOM with new data

本文关键字:DOM 数据 或者 新数据 刷新 加载 jQueryAJAX 新加载      更新时间:2023-09-26

我有一段代码,在用户从另一个页面选择按钮后,使用jQuery将内部网页中的内容加载到新的页面DOM中。我遇到的问题是,无论我单击哪个选项按钮,DOM中都会显示相同的结果。我被告知要把Var改为live,但这并没有什么不同。除了有人在看我的代码之外,我真的无法解释更多。处理这个问题的最佳方法是什么?

<html>
<head>
<meta charset="utf-8">
<title>jQuery Mobile Web App</title>
<link href="../jquery.mobile.theme-1.0.min.css" rel="stylesheet" type="text/css"/>
<link href="../jquery.mobile.structure-1.0.min.css" rel="stylesheet" type="text/css"/>
<script src="../jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="../jquery.mobile-1.0.min.js" type="text/javascript"></script>
</head> 
<body> 
<div data-role="page" id="page">
    <div data-role="header">
        <h1>Page One</h1>
    </div>
    <div data-role="content">   
        <ul data-role="listview">
            <li><a href="#options">Options</a></li>
            <li><a href="#results">results</a></li>
        </ul>       
    </div>
    <div data-role="footer">
        <h4>Page Footer</h4>
    </div>
</div>
<div data-role="page" id="options">
    <div data-role="header">
        <h1>Options</h1>
    </div>
    <div data-role="content">   
        <div id="page-wrap">
            <div id="header">
                <h1>Call a webpage in a DOM</h1>
            </div>
            <div id="load-div" class="functions">
                <span>Value 1</span>
                <input type="submit" value="300GB" id="load" />
<div id="load-div" class="functions">
                <span>Value 2</span>
                <input type="submit" value="500Gb" id="load2" />
            </div>
            <a href="#results">results</a>      
        </div>  
    </div>
    <div data-role="footer">
        <h4>Page Footer</h4>
    </div>
</div>
</div>



<div data-role="page" id="results">
    <div data-role="header">
        <h1>Results</h1>
    </div>
    <div data-role="content">
                    <div id="result"  class="functions">
</div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
    $.ajaxSetup ({
        cache: false
    });
    var ajax_load = "<img class='loading' src='img/load.gif' alt='loading...' />";
//  load() functions
    var loadUrl = "load.html";
    $("#load").live("click",function(){
        $("#result").html(ajax_load).load(loadUrl);
    });
</script>

<script type="text/javascript">
    $.ajaxSetup ({
        cache: false
    });
    var ajax_load = "<img class='loading' src='img/load.gif' alt='loading...' />";
//  load() functions
    var loadUrl = "load2.html";
    $("#load_2").live("click",function(){
        $("#result").html(ajax_load).load(loadUrl);
    });
</script>
<a href="#options">Options</a>  
    </div>
    <div data-role="footer">
        <h4>Page Footer</h4>
    </div>
</div>
</body>
</html>

基本上,您可以覆盖这里的每个脚本。不要使用loadUrl

对于#load:

$("#load").live("click",function(){
    $('#result').html(ajax_load).load('load.html');
});

对于#load2:

$("#load_2").live("click",function(){
    $("#result").html(ajax_load).load(`load2.html`);
});

单独的脚本标记不是独立的。一个代码中的代码可以与另一个代码交互。

您已经在两个块中声明了var ajax_load和loadUrl。第二个区块中的区块会覆盖第一个区块。要么给它们不同的名称,要么去掉它们并将字符串值直接放在函数中。