jQuery解析ajax响应数据并获取元素id -值

时间:2022-10-09 11:18:18

I have a jQuery ajax get that will return an html text. From this one I need to extract the h3 element (with id='title') value: THIS IS TITLE.

我有一个jQuery ajax get返回一个html文本。我需要从中提取h3元素(id='title')值:这是title。

Can I achieve this with jQuery?

我能用jQuery实现这一点吗?

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

and here is the call:

这里有个电话:

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

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

      }
  });

2 个解决方案

#1


8  

Use find() method to get the value like below,

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

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

An Example for how to use find is here How to Use find()

如何使用find()的示例如下

#2


2  

Use RegExp:

使用正则表达式:

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]);

#1


8  

Use find() method to get the value like below,

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

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

An Example for how to use find is here How to Use find()

如何使用find()的示例如下

#2


2  

Use RegExp:

使用正则表达式:

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]);