简体   繁体   English

如何使用 javascript 切换元素

[英]How to toggle elements with javascript

I'm working in wordpress with the woocommerce plugin where I wan't to show two different prices on my products.我正在使用 woocommerce 插件在 wordpress 中工作,我不想在我的产品上显示两种不同的价格。

One as default that is shown as the user gets to the page and one that's shown when the user clicks a button.一种是在用户访问页面时显示的默认值,另一种是在用户单击按钮时显示。 If he clicks the same button again the previous price should show again.如果他再次单击相同的按钮,则应再次显示先前的价格。 And so on.等等。

So some type of check of what is shown on the page, which could be done in JQuery I guess, but is there any other way?所以对页面上显示的内容进行某种类型的检查,我猜可以在 JQuery 中完成,但还有其他方法吗? I tried a bit with JQuery but have a bit of problems using it with woocommerce.我尝试了一些 JQuery,但在使用 woocommerce 时遇到了一些问题。

<?php
$incl = $product->get_price_including_tax();?>

<p id="excl" class="price"><?php echo $product->get_price_excluding_tax();?> excl.</p>

<button type="button" onclick="document.getElementById('excl').innerHTML = <?php echo $incl;?>">Click Me!</button>

That does the one way around but needs the previous mentioned check in order to be used the other way around again.这是一种方式,但需要前面提到的检查才能再次使用另一种方式。

And a extra question: those incl and excl tax call ruin the settings done in the admin end with the formatting of prices.还有一个额外的问题:那些 incl 和 excl tax call 破坏了在管理端完成的设置,价格的格式。 So 6.000,00 for instant becomes 6000. I tried woocommerce_price which works but is deprecated.所以 6.000,00 瞬间变成了 6000。我试过 woocommerce_price,它有效但已被弃用。 Is there a similar function to do that?有没有类似的功能来做到这一点? Can't find anything in the documentation.在文档中找不到任何内容。

Use ternary operator, condition ? expr1 : expr2使用ternary运算符, condition ? expr1 : expr2 condition ? expr1 : expr2

The conditional (ternary) operator is the only JavaScript operator that takes three operands.条件(三元)运算符是唯一一个接受三个操作数的 JavaScript 运算符。 This operator is frequently used as a shortcut for the if statement[ Reference ]此运算符经常用作 if 语句的快捷方式[ 参考资料]

Note: Do not mix-up your logic in markup.注意:不要在标记中混淆你的逻辑。 You should have a separate function for that.你应该有一个单独的功能。

 <p id="excl" class="price">100</p> <button type="button" onclick="document.getElementById('excl').innerHTML=document.getElementById('excl').innerHTML=='500'?'100':'500'">Click Me!</button>

I would write them both to DOM and use toggleClass() from jQuery我会将它们都写入 DOM 并使用 jQuery 中的toggleClass()

<?php
$incl = $product->get_price_including_tax();?>
<div class="mycont">
    <p id="excl" class="price excl"><?php echo $product->get_price_excluding_tax();?> excl.</p>
    <p id="incl" class="price incl"><?php echo $incl;?> excl.</p>
</div>
<button type="button">Click Me!</button>

With following CSS:使用以下 CSS:

.mycont > .price.incl {display: none}
.mycont.show_incl > .price.incl {display: block}
.mycont.show_incl > .price.excl {display: none}

And jQuery:和 jQuery:

$('button').on('click', function(){
    $('.mycont').toggleClass('show_incl');
});

Fiddle Here在这里小提琴

If you want to do it in pure javascript, I would suggest to call a function onClick event on button, and toggle class like stated here如果你想用纯 javascript 来做,我建议在按钮上调用一个函数onClick事件,并像这里所说的那样切换类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM