javascript - 如何使喜欢和添加到购物车图标切换外观

标签 javascript jquery html css fbsdklikebutton

我正在尝试运行一个简单的 like 和 addtocart 功能,当用户点击图标时,它会立即切换到填充或不同颜色的图标。但是,父 div 的链接也被继承,所以我在脚本中添加了一个 stopPropagation。我还没有为#addtocart 添加 html 和脚本代码,因为该功能将类似于#heart like 图标。

问题是,开关不起作用 + 我的编辑器给了我一个“错误:$undefined”消息,即使我正确地链接了我的 javascript。如果有人可以帮助指出我哪里出错了,我将不胜感激。我最终想添加点击图标将产品添加到用户的愿望 list 或购物车页面的功能,但我还没有学习如何编写后端代码,所以我现在只渲染 UI。但是,如果您能告诉我如何使图标链接到用户的愿望 list 或购物车页面而不继承父链接,那就太好了。

$(document).ready(function() {
  $('#heart').click(function() {
    $('#heart--liked').toggle('1000');
  });
  $("#heart a").click(function(e) {
    //do something to stop link # from loading
    e.stopPropagation();
  });
});
.product__list__item {
  border-radius: 2px;
  background-color: var(--pure-white);
  border: solid 1px var(--dark-grey);
}

.product__list__item--description {
  padding: 1.5rem;
  text-align: left;
}

.product__list__item h3 {
  margin-bottom: .75rem;
  position: relative;
  display: block;
}

.product__list__item--icons {
  display: inline-block;
  float: left;
  padding-top: 5px;
}

.product__list__item--icons a {
  color: var(--middle-grey);
}

.product__list__item--icons a:hover {
  color: var(--dark-grey);
}

#heart--liked {
  display: none;
  transition: .2s;
}

#addtocart {
  margin-left: 10px;
}

.product__list__item--price {
  display: inline-block;
  float: right;
  color: var(--dark-gray);
  font-size: 1.5rem;
  text-align: right;
  margin-top: 50px;
}

.price--hot {
  color: var(--crimson-red);
}

.price--display {
  color: var(--pompeian-pink);
}

.product__list__item--description h3 sup {
  font-size: .875rem;
  padding: 5px;
  text-transform: uppercase;
  vertical-align: super;
}

.price__sale {
  text-decoration: line-through;
  font-size: 1.25rem;
  color: var(--highlight-gray);
  margin-left: 5px;
  vertical-align: sub;
}

.product__list__item--description:after {
  content: "";
  display: table;
  clear: both;
}
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">


</head>

<body>
  <div class="product__list__item">
    <a href="#" class="image-container">
      <div class="image-container__wrapper">
        <div class="image-container--mask">
          <h4>View details</h4>
        </div>
        <img src="https://foter.com/photos/235/design-tree-home-acapulco-lounge-chair-yellow-1.jpg?s=pi" alt="yellow chair">
      </div>
      <div class="product__list__item--description">
        <h3>Yellow Chair<sup class="price--hot">Clearance!</sup></h3>
        <div class="product__list__item--icons">
          <span id="heart"><a href=" "><i class="far fa-heart fa-2x"></i><i id="heart--liked" class="fas fa-heart fa-2x"></i></a></span>
          <span id="addtocart"><a href=" "><i class="fas fa-cart-plus fa-2x"></i></a></span>
        </div>
        <div class="product__list__item--price price--hot">$189
          <sub class="price__sale">$109</sub>
        </div>
      </div>
    </a>

  </div>

</body>

</html>

最佳答案

我建议你使用一个类来改变点击时的颜色。

请参阅下面的工作片段,其中我试图尊重您的做事方式:
我在正文中添加了 CSS 变量以使其工作,并在我添加/修改的地方添加了一些注释。

$(document).ready(function() {

  // Specific code for the heart fill toggle
  $("#heart--liked").click(function(e) {
    $(this).toggleClass("far").toggleClass("fas"); // Toggle the filling !
  });

  // Action when click on a link (color)
  $(".product__list__item--icons a").click(function(e) {
    e.preventDefault(); // Modified: stop link # from loading (Why using link then?)
    $(this).toggleClass("selected"); // Toggle the colored class !
  });

});
.product__list__item {
  border-radius: 2px;
  background-color: var(--pure-white);
  border: solid 1px var(--dark-grey);
}

.product__list__item--description {
  padding: 1.5rem;
  text-align: left;
}

.product__list__item h3 {
  margin-bottom: .75rem;
  position: relative;
  display: block;
}

.product__list__item--icons {
  display: inline-block;
  float: left;
  padding-top: 5px;
}

.product__list__item--icons a {
  color: var(--middle-grey);
}

.product__list__item--icons a:hover {
  color: var(--dark-grey);
}


/* Added the two below */

.product__list__item--icons .selected {
  color: var(--light-pink);
}

.product__list__item--icons .selected:hover {
  color: var(--pink);
}

#heart--liked {
  /*display: none; REMOVED */
  transition: .2s;
}

#addtocart {
  margin-left: 10px;
}

.product__list__item--price {
  display: inline-block;
  float: right;
  color: var(--dark-gray);
  font-size: 1.5rem;
  text-align: right;
  margin-top: 50px;
}

.price--hot {
  color: var(--crimson-red);
}

.price--display {
  color: var(--pompeian-pink);
}

.product__list__item--description h3 sup {
  font-size: .875rem;
  padding: 5px;
  text-transform: uppercase;
  vertical-align: super;
}

.price__sale {
  text-decoration: line-through;
  font-size: 1.25rem;
  color: var(--highlight-gray);
  margin-left: 5px;
  vertical-align: sub;
}

.product__list__item--description:after {
  content: "";
  display: table;
  clear: both;
}
<html>

<head>
  <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">
</head>

<body style="--middle-grey: #888; --dark-grey: #444; --light-pink: #ffb6c1; --pink: #dd888b;">
  <div class="product__list__item">
    <a href="#" class="image-container">
      <div class="image-container__wrapper">
        <div class="image-container--mask">
          <h4>View details</h4>
        </div>
        <img src="https://foter.com/photos/235/design-tree-home-acapulco-lounge-chair-yellow-1.jpg?s=pi" alt="yellow chair">
      </div>
      <div class="product__list__item--description">
        <h3>Yellow Chair<sup class="price--hot">Clearance!</sup></h3>
        <div class="product__list__item--icons">
          <span id="heart"><a href=" "><i id="heart--liked" class="far fa-heart fa-2x"></i></a></span>
          <span id="addtocart"><a href=" "><i class="fas fa-cart-plus fa-2x"></i></a></span>
        </div>
        <div class="product__list__item--price price--hot">$189
          <sub class="price__sale">$109</sub>
        </div>
      </div>
    </a>
  </div>
</body>

</html>

希望对你有帮助。

关于javascript - 如何使喜欢和添加到购物车图标切换外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50150537/

相关文章:

javascript - PhpStorm:未解析的变量或类型控制台

javascript - jQuery - 选择输入字段的关联标签元素

javascript - 顺时针循环一组数字

javascript - AJAX - 我需要在服务器端返回完整的 HTML 文档吗?

javascript - 用于切换 css 模板 Bootstrap 的按钮

javascript - jqueryparent().find(input).val();单击时访问一个表格单元格中的两个输入字段

javascript - 禁用下拉选项

javascript - 对测试进行评分 - 循环检查 radio 输入值 (JS)

html - 响应式设计 : Site not shrinking past 763px

javascript - 不在焦点时隐藏文本区域字段