简体   繁体   中英

get value of inside a tag with jQuery.?

How can by jQuery get value inside tag b ?

<span>
 <b>hi_1</b>
 <b>hi_2</b>
 <b>hi_3</b>
 <b>hi_4</b>
<span>

I want this output with jQuery: hi_1, hi_2, hi_3, hi_4

Please give me example in jsfiddle .

To get the value inside a specific HTML tag in jQuery you can use the text function. This combined with a selector gets the output you're looking for

$('span b').each(function() {
  console.log($(this).text());
});

JSFiddle

JSFiddle with commas

Are you looking for something like this?

http://jsfiddle.net/ZDYnq/

$(document).ready(function() {
   var textArr = [];
   $('span b').each(function() {
     textArr.push($(this).text());
   });
    alert(textArr.join(', '));
});

This is cool

var x = $("span b").map(function() {
  return $(this).text();
}).toArray().join(", "); 

Demo here

Discussed here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM