【问题标题】:Return inner html value of the ajax response html返回ajax响应html的内部html值
【发布时间】:2021-06-03 06:46:19
【问题描述】:

我正在尝试通过 ajax 响应返回 html 页面,如果我发出警报,响应会返回页面的 html 代码。我想阅读那些 html 代码的内部 html 来获取和值/内容。 如果 ajax 请求成功,我该如何实现。 提前致谢。

// ajax

    let req = $.ajax({
        url: '/page',
        method: 'GET',
        dataType: 'html'
    });
    req.done(function(response){
        // console.log(response)
        // $(response).find('title').html();
        // $(response).find('.my-header').html();
        alert(response);
    });

//html响应

<head>
    <title>my pagename</title>
</head>
<body>
    <div class="my-header">HEADER TEXT</div>
</body>

【问题讨论】:

  • 这能回答你的问题吗? Parse an HTML string with JS
  • 你目前的尝试有什么问题?
  • 我想读取通过响应返回的标题内容。我试过 // $(response).find('title').html();但没用
  • @OneBuyu - 试试 $(response).find('Title').html();

标签: javascript jquery ajax


【解决方案1】:

尝试使用filter() 而不是find()。 jQuery 去掉整页的头部

const title = $(response).filter('title').text();

【讨论】:

  • 非常感谢@charlietfl。我现在工作。
猜你喜欢
  • 2018-03-17
  • 2015-11-10
  • 2017-07-03
  • 2019-01-09
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 2013-01-28
  • 2016-03-16
相关资源
最近更新 更多