2014-07-24 45 views
5

我有一個jQuery ajax get,它將返回一個html文本。從這一個我需要提取h3元素(ID ='標題')值:THIS IS TITLEjQuery解析ajax響應數據並獲取元素id - 值

我可以用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); 

     } 
    }); 
+0

也許使用find我猜? –

+3

'$(data).find('#title')。text()' – undefined

+0

由於Id是唯一的,所以可以使用$('#title').text() –

回答

8

使用find()方法方法來獲得像下面的值,

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

如何使用找到一個例子是在這裏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]);