admin 管理员组

文章数量: 1086019

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.

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.

Share Improve this question asked Aug 31, 2011 at 14:19 Kate ThompsonKate Thompson 4412 gold badges7 silver badges13 bronze badges 1
  • -1 for not reading the documentation and trying something yourself – user142162 Commented Aug 31, 2011 at 14:21
Add a ment  | 

3 Answers 3

Reset to default 3

Are you looking for something like this?

http://jsfiddle/ZDYnq/

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

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

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

JSFiddle

JSFiddle with mas

This is cool

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

Demo here

Discussed here

本文标签: javascriptget value of inside a tag with jQueryStack Overflow