开发者

get value of inside a tag with jQuery.?

开发者 https://www.devze.com 2023-04-01 18:09 出处:网络
How can by jQuery get value inside tag b? <span> <b>hi_1</b> <b>hi_2</b>

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.


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(', '));
});


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


This is cool

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

Demo here

Discussed here

0

精彩评论

暂无评论...
验证码 换一张
取 消