简体   繁体   English

Jquery - Ajax 使用 load() ZC1C425268E68385D1AB5074F4C1 加载返回的 html 的一部分

[英]Jquery - Ajax load a portion of returned html using load() function

Please I need some help here.请我在这里需要一些帮助。 I am trying to make some ajax calls to my django server to enable me return a queryset.我正在尝试对我的 django 服务器进行一些 ajax 调用,以使我能够返回查询集。 I believe the call is successful but the problem is in the returned data contains the whole html and I have tried selecting the div of interest to me but it fails every time.我相信调用成功,但问题在于返回的数据包含整个 html 并且我尝试选择我感兴趣的 div 但每次都失败。

Here is the successful call without div selection:这是没有 div 选择的成功调用:

$(document).ready(function () {
    var review_div = $('#tm-review-rating-star');
    var next = $('#next-page');
    var prev = $('#previous-page');

    next.click(function() {
        var url = $(this).attr('href');
        console.log(url)
        console.log(url)
        review_div.load(url);

        return false;
    })
});

Once I try to select the div of interest, the call returns an error please can anyone help me the code for the selection attempt is below:一旦我尝试 select 感兴趣的 div,调用返回错误请任何人都可以帮我选择尝试的代码如下:

$(document).ready(function () {
    var review_div = $('#tm-review-rating-star');
    var next = $('#next-page');
    var prev = $('#previous-page');

    next.click(function() {
        var url = $(this).attr('href');
        console.log(url)
        console.log(url)
        review_div.load(url + review_div);

        return false;
    })
});

Here is the pages html:以下是 html 页面:

    {% load static %}
{% load auth_extras %}
<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .tm-nav li a:hover{
            background-color: #b7d8f7 !important; 
                            
          }

          .tm-nav li a{
            color: darkblue !important;
            font-weight: 800; 
            
          }
    </style>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="apple-touch-icon" sizes="57x57" href="{% static '/iconfavs/apple-icon-57x57.png'%}">
    <link rel="icon" type="image/icon" href="{% static '/iconfavs/favicon.ico'%}">
    <link rel="stylesheet" type="text/css" href="{% static '/css/styles.css'%}">
    <link rel="stylesheet" type="text/css" href="{% static '/css/slideshow.css'%}">
    <link rel="stylesheet" type="text/css" href="{% static '/css/slideshowadvert.css'%}">
    <link rel="apple-touch-icon" sizes="60x60" href="{% static '/iconfavs/apple-icon-60x60.png'%}">
    <link rel="apple-touch-icon" sizes="72x72" href="{% static '/iconfavs/apple-icon-72x72.png'%}">
    <link rel="apple-touch-icon" sizes="76x76" href="{% static '/iconfavs/apple-icon-76x76.png'%}">
    <link rel="apple-touch-icon" sizes="114x114" href="{% static '/iconfavs/apple-icon-

jaas=贾斯=

See the documentation for .load()请参阅.load()的文档

Loading Page Fragments加载页面片段

... If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded ...如果字符串中包含一个或多个空格字符,则假定字符串中第一个空格之后的部分是确定要加载的内容的 jQuery 选择器

If you just want the contents within the #tm-review-rating-star element, it sounds like you want something like this如果你只想要#tm-review-rating-star元素中的内容,听起来你想要这样的东西

review_div.load(`${this.href} #tm-review-rating-star > *`)

The selector #tm-review-rating-star > * tells jQuery to get everything inside the #tm-review-rating-star element but not the element itself.选择器#tm-review-rating-star > *告诉 jQuery 获取#tm-review-rating-star元素中的所有内容,但不获取元素本身。

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

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