我们可以使用 JQuery 让加载的页面将不同的页面加载到第一页中的元素吗?

Can we make a loaded page load a different page to an element in the first page using JQuery?

我想让一个元素在另一个已加载页面中的事件被触发时加载一个页面。我认为问题是加载的页面无法识别加载它的页面中的元素。有什么解决办法吗?这是我的代码:

Page1.php:

    <script type="text/javascript">
    $(document).ready(function(){
        $(".class1").load("page2.php"); 
    });
    </script>
    <div class="class1"></div>
    <div class="classfrompage1"></div>

Page2.php:

$(".btnpage2").click(function(){
        $.ajax({
            //Some ajax code to write to database
            success:function(data){                 
            $(".classfrompage1").load("page3.php");
            }
        });
});

提前致谢。

由于您的内容是动态加载的,您可以尝试

$(document).on("click",".btnpage2",function(){ 
  $.ajax({ success:function(data){ 
  //Some ajax code to write to database
    $(".classfrompage1").load("page3.php"); 
    } 
  }); 
});

替代方法是将您在第 2 页中的代码传递给第 1 页中加载的回调函数