【问题标题】:jQuery parse ajax response data and get element id - valuejQuery 解析 ajax 响应数据并获取元素 id - 值
【发布时间】:2015-12-02 13:12:53
【问题描述】:

我有一个 jQuery ajax get,它将返回一个 html 文本。我需要从中提取 h3 元素(id='title')值:THIS IS TITLE

我可以用 jQuery 实现吗?

<div class="content">
   <h3 id="title">THIS IS TITLE</h3>
   .....
</div>

这是电话:

  $.ajax({
      url: url,
      type: 'GET',
      cache:false,
      success: function (data) { // data is the html text returned

       alert('The title is: ' + TITLE HERE);

      }
  });

【问题讨论】:

  • 也许使用 find 我猜?
  • $(data).find('#title').text()
  • 由于Id是唯一的,可以使用$('#title').text()

标签: jquery ajax


【解决方案1】:

使用 find() 方法获取如下值,

$(data).find('#title').text()

这里有一个关于如何使用 find 的示例How to Use find()

【讨论】:

    【解决方案2】:

    使用正则表达式:

    text='<div class="content"><h3 id="title">THIS IS TITLE</h3>.....</div>';
    console.log(text.match(/<h3 id="title">(.*?)<\/h3>/)[1]);// or alert:
    alert('The title is: ' + text.match(/<h3 id="title">(.*?)<\/h3>/)[1]);
    

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多