jQuery addClass 或包裹在元素不太可能的部分

发布于 2024-12-02 02:40:12 字数 770 浏览 1 评论 0原文

我真的很讨厌我的购物车的设置方式,因为我无法编辑大部分页面。所以我求助于使用 jQuery,这既有趣又令人沮丧,因为我们必须使用太多它。话虽这么说..我正在尝试通过 jQuery 找到一种方法来专门针对两种情况的价格添加一个类。不幸的是,价格没有设置为专门为其添加一个类(我无法理解为什么我的购物车会这样做!)。任何帮助将不胜感激!

更新:也许我需要使用wrap()方法来包装价格并给它一个类?

<-- Situation 1 -->
<b>
  <font class="text colors_text">
  <b>Regular Price: </b>
  </font>
   $2,533.31
</b> 

<-- Situation 2 -->
<b>
  <span class="exclusive">Exclusive Price:</span>
  <font class="pricecolor colors_productprice">$2,343.30</font>
</b>

我不会详细解释为什么我需要为这些价格添加一个类,因为类名背后有很多内容,可以使页面的大部分内容正确呈现。每种情况的类名应该相同。情况 2 我可能可以轻松处理 $('.exclusive').next('font').addClass('priceis') 但情况 1 相当困难。

I really hate the way my shopping cart is set up as I CANNOT edit much of the pages. So I resort to using jQuery which is both fun and frustrating because we have to use so much of it. That being said..I am trying to find a way through jQuery to add a class specifically for the price given two situations. Unfortunately the price is not set up to add a class to it specifically (why my shopping cart did it this way is beyond me!). Any help would be appreciated!

UPDATE: Perhaps I need to use wrap() method to wrap the price and give it a class?

<-- Situation 1 -->
<b>
  <font class="text colors_text">
  <b>Regular Price: </b>
  </font>
   $2,533.31
</b> 

<-- Situation 2 -->
<b>
  <span class="exclusive">Exclusive Price:</span>
  <font class="pricecolor colors_productprice">$2,343.30</font>
</b>

I'm not going to dwell into the reasons why I need to add a class to these prices because there is a lot riding behind the class name which enables much of the page to render correctly. The class names for each situation should be the SAME. Situation 2 I may be able to handle with ease $('.exclusive').next('font').addClass('priceis') but Situation 1 is pretty tough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2024-12-09 02:40:12

对于情况一,您可以这样做:

var font = $(".text");//get the font tag
$(font.get(0).nextSibling).wrap('<span class="priceis" />'); 
//wrap it's next sibling inside a span

这会在所需类别的价格周围添加一个跨度

For situation one you could do:

var font = $(".text");//get the font tag
$(font.get(0).nextSibling).wrap('<span class="priceis" />'); 
//wrap it's next sibling inside a span

this add a span around the price with the desired class

少女情怀诗 2024-12-09 02:40:12
// get first "b" element
var b = $("b:eq(0)");

// get font element
var font = b.find("font:eq(0)");

// clone font element
var fontClone = font.clone();

// remove font element
font.remove();

// get remaining text (the price)
var price = b.html();

// replace "b" tag's html with span with class "priceis" containing price
b.html("<span class=\"priceis\">" + price + "</span>");

// put the font clone back in
b.preprend(fontClone);
// get first "b" element
var b = $("b:eq(0)");

// get font element
var font = b.find("font:eq(0)");

// clone font element
var fontClone = font.clone();

// remove font element
font.remove();

// get remaining text (the price)
var price = b.html();

// replace "b" tag's html with span with class "priceis" containing price
b.html("<span class=\"priceis\">" + price + "</span>");

// put the font clone back in
b.preprend(fontClone);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文