2014-07-24 9 views
5

Ho un jQuery ajax get che restituirà un testo html. Da questo ho bisogno di estrarre il valore dell'elemento h3 (con id = 'titolo'): THIS IS TITLE.jQuery analizza i dati di risposta ajax e ottiene l'id dell'elemento - value

Posso raggiungere questo obiettivo con jQuery?

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

e qui è la chiamata:

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

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

     } 
    }); 
+0

Forse usando trova, credo? –

+3

'$ (data) .find ('# title'). Text()' – undefined

+0

Poiché Id è univoco, puoi usare $ ('# title'). Text() –

risposta

8

Usa find() metodo per ottenere il valore come qui di seguito,

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

un esempio per come utilizzare ritrovamento è qui How to Use find()

2

Utilizza 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]); 
Problemi correlati