真的需要一些帮助来解决这个问题!
我尝试使用 jQuery 检索 HTML 的一部分,但出现错误:
Null is not an object on
此行导致错误:
$("#collection").html(jQuery(html).find('#collection').html());
function update_products(ajax_page) {
$.ajax({
type: "GET",
url: ajax_page,
data: {},
success: function(html) {
$("#collection").html(jQuery(html).find('#collection').html());
history.pushState({
page: ajax_page
}, "", ajax_page);
}
});
}
检索整个页面时它起作用:
function update_products(ajax_page) {
$.ajax({
type: "GET",
url: ajax_page,
data: {},
success: function(html) {
$("#collection").html(html);
history.pushState({
page: ajax_page
}, "", ajax_page);
}
});
}
我认为它与jQuery(html).find('#collection').html()
有关,但我不知道哪里出了问题。
其他版本出错:
Null is not an object On this line `var $response=$(html);'
function update_products(ajax_page) {
$.ajax({
url: ajax_page,
dateType: 'html',
success: function(html){
//Create jQuery object from the response HTML.
var $response=$(html);
//Query the jQuery object for the values
var collection = $response.filter('#collection').text();
$("#collection").html(collection);
history.pushState({
page: ajax_page
}, "", ajax_page);
}
});
最佳答案
在之前的项目中,我做了类似以下的事情:
$("#collection").load(ajax_page+" #collection", ajax=true, function(){
history.pushState({
page: ajax_page
}, "", ajax_page);
});
当然,我的项目没有相同的 ID,并且我的 url 不在变量中,它是常量,所以我只是将其写入。
我在返回的数据中对我的项目做了进一步的工作,但我将其放入隐藏的 DIV 中,因此在我操作它之前它实际上是页面的一部分。这对你来说不起作用,因为 ID 会在页面上出现两次。没有它也许是可能的,但我并没有为了找出答案而奋斗。这些天我将进入 #collection
div 的处理工作转移到一个单独的脚本中,这样当您想要刷新它时更容易使用 ajax。
关于jquery - Null 不是 AJAX 的对象并检索部分页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52367745/