如何使用jquery获取span标签中的文本?

时间:2021-05-12 20:30:15

I want get text within this span tag using its class:

我希望使用其类在此span标记中获取文本:

<span id="prizevalue_g12" class="pull-right grid_val">£12</span>

I want to get 12. I tried the below code:

我想得到12.我尝试了下面的代码:

var ex_price = $(".grid_val").html();

and

ex_price = $(".grid_val").html();

1 个解决方案

#1


0  

You can use replace() to remove £.

您可以使用replace()来删除£。

$(document).ready(function(){
  var ex_price = $(".grid_val").html();
  ex_price = $(".grid_val").html();  
  var ans=ex_price.replace('£', '') ;
  alert(ans); 
});

This is just a tought, but I think this would be a perfect place to use some custom data-attributes, like data-value. It would work nice especially if you have a lot of these values to get.

这只是一个尝试,但我认为这将是一个使用一些自定义数据属性,如数据值的理想场所。它会很好用,特别是如果你有很多这些值。

The outcome would be smthn like this:

结果会像这样结果:

<!-- HTML -->
<span class="grid_val" data-value="12">£12</span>
<span class="grid_val" data-value="15">£15</span>
<span class="grid_val" data-value="13">£13</span>
<span class="grid_val" data-value="1">£1</span>

<script>
$(".grid_val").each(function(){
  var value = $(this).attr("data-value");
  //do something with it
});
</script>

#1


0  

You can use replace() to remove £.

您可以使用replace()来删除£。

$(document).ready(function(){
  var ex_price = $(".grid_val").html();
  ex_price = $(".grid_val").html();  
  var ans=ex_price.replace('£', '') ;
  alert(ans); 
});

This is just a tought, but I think this would be a perfect place to use some custom data-attributes, like data-value. It would work nice especially if you have a lot of these values to get.

这只是一个尝试,但我认为这将是一个使用一些自定义数据属性,如数据值的理想场所。它会很好用,特别是如果你有很多这些值。

The outcome would be smthn like this:

结果会像这样结果:

<!-- HTML -->
<span class="grid_val" data-value="12">£12</span>
<span class="grid_val" data-value="15">£15</span>
<span class="grid_val" data-value="13">£13</span>
<span class="grid_val" data-value="1">£1</span>

<script>
$(".grid_val").each(function(){
  var value = $(this).attr("data-value");
  //do something with it
});
</script>