javascript - 单击时切换 div(更喜欢 CSS,但 Jquery 也可以)

标签 javascript jquery html css

这几天一直困扰着我。我正试图为此找到一个纯 CSS 解决方案,但 JQUERY 也很好。我希望在我的页面上默认有一个每月价格表,并在有人点击链接时立即将表格切换为“每年”,反之亦然。

这是 HTML 标记:

<div class="pricing_table" id="monthly">
   <ul>
     <li>Basic</li>
     <li><a href="" class="buy_now">Subscribe Now</a></li>
   </ul>
   <ul style="background-color:#CCCCCC;">
   <h2>Monthly Plans</h2><a>Click here to switch to the "Yearly Plans"</a></ul>
</div>
<div class="pricing_table" id="yearly">
   <ul>
     <li>Basic</li>
     <li><a href="" class="buy_now">Subscribe Now</a></li>
   </ul>
   <ul style="background-color:#CCCCCC;">
   <h2>Yearly Plans</h2><a>Click here to switch to the "Monthly Plans"</a></ul>
</div>

提前致谢

更新:

这是解决方案的链接 http://jsfiddle.net/8TRAe/1/

特别感谢 Tamil Vendhan

最佳答案

首先为您的链接分配 ID

<a id="yearlyPlan">Click here to switch to the "Yearly Plans"</a>

<a  id="monthlyPlan">Click here to switch to the "Monthly Plans"</a>

你的JS:

$("#yearlyPlan").click(function(event) {
  $("#monthly").hide();
  $("#yearly").show();
});

$("#monthlyPlan").click(function(event) {
  $("#monthly").show();
  $("#yearly").hide();
});

I would like to have a monthly price table by default on my page

为此,请在您的 HTML 中将 style="display:none" 设置为每年

<div class="pricing_table" id="yearly" style="display:none">

关于javascript - 单击时切换 div(更喜欢 CSS,但 Jquery 也可以),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17389442/

相关文章:

javascript - javascript 的窗口宽度条件

javascript - 在当前点击的TD中打开div

css - 努力用模板中的 Logo 替换 Font Awesome 图标

html - CSS组合relative+fixed无跳转效果

javascript - 如何使用 Youtube Api 获取用户的喜欢、评论和观看历史记录?

javascript - 无法加载资源,即使它在那里

jquery - 将类添加到动态高度 div

javascript - 如何根据当前事件日期突出显示元素?

javascript - 尝试使用带参数的函数分别更改两个图标

php - 从 html 重定向到 php