Jquery 从具有相同类的跨距中获取文本
我有5个跨度,可能更多,所有跨度都属于Jquery 从具有相同类的跨距中获取文本,jquery,Jquery,我有5个跨度,可能更多,所有跨度都属于。price 阅读Jquery文档,我发现最好的方法是使用map函数。像这样: var prices = $(".price") .map(function () { return this.text; }).get().join(); 我需要一份价目表,这样以后我可以做(注意 减20部分): 但是当我提醒价格时,我得到一个空窗口 html: <span class="
。price
阅读Jquery文档,我发现最好的方法是使用map
函数。像这样:
var prices = $(".price")
.map(function () {
return this.text;
}).get().join();
我需要一份价目表,这样以后我可以做(注意
减20部分):
但是当我提醒价格时,我得到一个空窗口
html:
<span class="price price_50">50</span>
<span class="price price_100">90</span>
<span class="price price_200">120</span>
<span class="price price_300">140</span>
<span class="price price_500">150</span>
50
90
120
140
150
内部映射此
引用DOM,它没有任何文本
属性。因此,将其转换为jQuery对象并使用text()
方法获取文本内容,或者从DOM对象获取textContent
属性
var价格=$(“.price”)
.map(函数(){
返回$(this.text();
}).get().join();
console.log(价格)
1.
2.
3.
4.
5
您需要在每个span中包含一些内容
<span class="price price_50">50</span>
<span class="price price_100">100</span>
<span class="price price_200">200</span>
<span class="price price_300">300</span>
<span class="price price_500">500</span>
<p id="list_price"></p>
$( "p" )
.append( $( ".price" ).map(function() {
return $( this ).text();
})
.get()
.join( ", " ) );
50
100
200
300
500
美元(“p”)
.append($(“.price”).map(函数(){
返回$(this.text();
})
.get()
.加入(“,”);
香草javascript使用innerText
,这就是jQuery引用中的text()
var价格=$(“.price”)
.map(函数(){
返回此.innerText;
}).get().join();
console.log(价格)
1.
2.
3.
4.
5
在元素存在之前或在元素有文本之前阅读都是很重要的。。。。。现在他们在示例中没有文本。。。dom也没有。text您是否正在尝试从price\u
类获取号码?
<span class="price price_50">50</span>
<span class="price price_100">100</span>
<span class="price price_200">200</span>
<span class="price price_300">300</span>
<span class="price price_500">500</span>
<p id="list_price"></p>
$( "p" )
.append( $( ".price" ).map(function() {
return $( this ).text();
})
.get()
.join( ", " ) );