2010-10-06 6 views
26

मैं निम्नलिखित एंकर टैगएंकर टैग से पाठ जाओ

<a href="http://www.google.com/">Google</a> 

मुझे पता है कि एक लंगर ले से href प्राप्त करने के लिए है:

alert($(this).attr("href")); 

लेकिन यह कैसे मैं लंगर से पाठ मिलता है टैग, यानी मैं "Google" कैसे प्राप्त करूं?

उत्तर

61

उपयोग इस के लिए .text():

alert($(this).text()); 

आप मार्कअप चाहता है तो (.text() टैग और इस तरह निकाल देता है), .html()

alert($(this).html()); 

का उपयोग इस मामले वहाँ कोई अंतर नहीं है, यदि इसके बजाय आप यह था:

<a href="http://www.google.com/">Google <span>(External)</span></a> 

तो फिर वहाँ होगा:

एचटीएमएल:

<div class="col-sm-2 jumbotron jumbotronUserRaces list-group list-group- 
userRaces" id="listGroupUserRaces"> 
       <a href="" class="list-group-item active">First item</a> 
       <a href="" class="list-group-item">Second item</a> 
       <a href="" class="list-group-item">Third item</a> 
      </div> 

$(this).text() //"Google (External)" 
$(this).html() //"Google <span>(External)</span>" 
+2

कैसे मैं केवल गूगल मिल सकता है? –

+0

केवल तत्काल पाठ प्राप्त करने के लिए कोई जवाब? –

0

.text() के रूप में उल्लेख का उपयोग कर, मैं सब लंगर मेरी कोड में संयुक्त टैग का पाठ मिला जे एस:

$("#listGroupUserRaces").click(function() { 
    alert($(this).text()); 
}); 

उत्पादन:

  • पहले आइटम
  • दूसरा आइटम
  • तीसरा आइटम
+0

मुझे केवल एंकर टैग के टेक्स्ट मान की आवश्यकता है जिस पर मैंने क्लिक किया था .. –