将jQuery公式应用于类中包含某些单词的单个div值

elcex8rz  于 2023-05-17  发布在  jQuery
关注(0)|答案(1)|浏览(180)

我有以下的HTML块:

<div class="product BLACKSHIRT">
<span class="displayPrice">£50</span>
</div>
<div class="product WHITESHIRT">
<span class="displayPrice">£40</span>
</div>
<div class="product GREENSHIRT">
<span class="displayPrice">£55</span>
</div>
<div class="product YELLOWSHIRT">
<span class="displayPrice">£50</span>
</div>
<div class="product BLACKJACKET">
<span class="displayPrice">£60</span>
</div>
<div class="product WHITESHORTS">
<span class="displayPrice">£20</span>
</div>
<div class="product BLACKTROUSERS">
<span class="displayPrice">£30</span>
</div>

我想做的是挑选出每个div的类名中有“SHIRT”或“JACKET”的一部分,然后勾出内部span的值-并将当前值加15,例如

<div class="product BLACKSHIRT">
<span class="displayPrice">£50</span>
</div>

成为

<div class="product BLACKSHIRT">
<span class="displayPrice">£65</span>
</div>

这可能吗?我对现有的html没有任何控制,并且知道需要剥离“£”以使值变为数字。

jk9hmnmh

jk9hmnmh1#

使用Attribute Contains Selector*=

$("[class*=SHIRT] .displayPrice, [class*=JACKET] .displayPrice").html((i, oldhtml) => '£' + (15 + parseFloat(oldhtml.substr(1))).toFixed(2))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product BLACKSHIRT">
  <span class="displayPrice">£50</span>
</div>
<div class="product WHITESHIRT">
  <span class="displayPrice">£40</span>
</div>
<div class="product GREENSHIRT">
  <span class="displayPrice">£55</span>
</div>
<div class="product YELLOWSHIRT">
  <span class="displayPrice">£50</span>
</div>
<div class="product BLACKJACKET">
  <span class="displayPrice">£60</span>
</div>
<div class="product WHITESHORTS">
  <span class="displayPrice">£20</span>
</div>
<div class="product BLACKTROUSERS">
  <span class="displayPrice">£30</span>
</div>

相关问题