javascript - Return the text of each span using JQuery - Stack Overflow

Im able to get the list of all spans having the attribute dir='somevalue', with the following

Im able to get the list of all spans having the attribute dir='somevalue', with the following jquery statement

$("span[dir='somevalue']")

I want the text of each span i.e. for example, <span dir='somevalue'> text </span>, i need the value 'text', and i want this for every span returned by the previous statement. Any way to do this using Jquery ?

Thank You

Im able to get the list of all spans having the attribute dir='somevalue', with the following jquery statement

$("span[dir='somevalue']")

I want the text of each span i.e. for example, <span dir='somevalue'> text </span>, i need the value 'text', and i want this for every span returned by the previous statement. Any way to do this using Jquery ?

Thank You

Share Improve this question asked Dec 20, 2009 at 17:26 TomTom 111 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

I think is a good case to use Traversing/map:

var values = $("span[dir='somevalue']").map(function(){
  return $(this).text();
}).get();

The values variable is now an Array that contains on each array element the inner text of each span matched by your selector.

If you want to concatenate all the values in a string, you can use Array.prototype.join:

var allValues = values.join(" "); // using a space as separator between values
$("span[dir='somevalue']").text()

will give you the concatenation of all the text. If you want an array and are using Javascript 1.8 you can use

$("span[dir='somevalue']")
    .get()
        .reduce(function (a,b) { return a.concat([$(b).text()]); }, [])

or

var values = [];
$("span[dir='somevalue']").each(function(){
    values.push($this.text());
});

a bit boring but easy to understand.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745284163a4620458.html

相关推荐

  • javascript - Return the text of each span using JQuery - Stack Overflow

    Im able to get the list of all spans having the attribute dir='somevalue', with the following

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信