删除并添加锚href jQuery

时间:2018-07-12 22:20:24

标签: jquery html5

我有一个由锚元素组成的图片库,在这些元素内有一个“快速查看”按钮,当我单击此按钮时,将显示一个弹出窗口。如果我没有单击按钮,而是单击该项目,那么每个项目都应该将我带到网站的不同部分。我面临的问题是,当我为这些锚点的href设置路径并单击快速查看按钮时,弹出窗口会迅速消失,并且我将转到我在锚点元素的href中设置的任何路径。

当我单击“ quickview”按钮时,我想从锚中删除href属性,然后在关闭弹出窗口时将其重新添加。我知道当我单击快速查看按钮时,可以使用$("a").removeAttr("href");来删除href,但是如果这样做,那么当我关闭弹出窗口时,锚点将不再具有href属性。

在删除href之前,我需要使用原始路径添加href,我喜欢其中40项,该如何解决呢?

注意:前2个项目的href带有指向其他位置的链接,如果您希望看到弹出窗口,请单击后2个项目中的一个。

Codepen:https://codepen.io/CodingGilbert/pen/KBpbLo

// Open popup shop item
  $('.quickview__icon').click(function() {
    $('.overlay').css({'opacity': '1', 'visibility': 'visible'});

    // Change popup clothing-item: img, name, price
    var imgid = "#"+$(this).attr('id').replace(/\s/g,'') + "Img";
    var en the anchors noimgsrc = $(imgid).prop('src');
    var price = document.getElementById($(this).attr('id').replace(/\s/g,'') + "Price").innerHTML;
    $('#clothingImg').prop('src', imgsrc);
    document.getElementById('clothingName').innerHTML = $(this).attr('id');
    document.getElementById("clothingPrice").innerHTML = price;
  });

  // Popup close
  $('#closeIcon').click(function() {
    $('.popup, .overlay').css({
      'opacity': '0',
      'visibility': 'hidden'});
      $('body').css('overflow', 'visible');

  })
* {
  margin: 0;
  padding: 0;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  font-size: 62.5%;
  -webkit-font-smoothing: subpixel-antialiased;
}

body {
  font-family: "Karla", sans-serif;
  font-weight: 400;
  line-height: 1.6;
  color: #222;
}

.products-container {
  display: flex;
}
.container {
  padding: 2rem 3rem;
}

.shop-item {
  width: calc(100% / 4);
  height: 100%;
  margin: 1rem;
  position: relative;
  display: flex;
  justify-content: center;
}

.shop-item__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.quickview {
  width: 100%;
  height: 100%;
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-content: center;
  padding-bottom: 2rem;
  color: #222;
  letter-spacing: 0.15rem;
  text-transform: uppercase;
  opacity: 0;
  transition: all ease-in-out 250ms;
}

.quickview:hover {
  opacity: 1;
}

.quickview__icon {
  font-size: 1rem;
  background-color: rgba(204, 204, 204, 0.9);
  padding: 0.7rem 1rem;
  align-self: center;
}

.quickview__info {
  font-size: 1.2rem;
  align-self: center;
  text-align: center;
}

.quickview__info--price {
  font-size: 1rem;
  color: rgba(61, 61, 61, 0.6);
}

.popup {
  height: 100vh;
  width: 100%;
  background-color: rgba(245, 239, 236, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  transition: all ease-in-out 250ms;
}

.popup__img {
  height: 95%;
}

.popup__close-icon {
  position: absolute;
  top: 1rem;
  right: 2.5rem;
  font-size: 3rem;
  color: #d1d1d1;
  cursor: pointer;
  transition: all ease-in-out 250ms;
}

.popup__close-icon:hover {
  color: rgba(61, 61, 61, 0.6);
}

.popup__close-icon-clothing {
  position: absolute;
  font-size: 3.6rem;
  font-weight: lighter;
  color: #222;
  top: -1rem;
  right: 1.5rem;
  cursor: pointer;
}

.overlay {
  position: fixed;
  overflow-y: scroll;
  overscroll-behavior: contain;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  opacity: 0;
  visibility: hidden;
  background-color: rgba(240, 240, 240, 0.6);
}

.popup-item {
  width: 75%;
  height: 150vh;
  background-color: #fff;
  margin: 5rem auto;
  display: flex;
  justify-content: center;
}

.product-info {
  padding: 5rem 3.5rem;
  letter-spacing: 0.1rem;
  color: #222;
}

.product-info--shop {
  padding: 0 3.5rem;
}

.product-info__price {
  font-size: 2.4rem;
  margin: 3rem 0;
  display: block;
  color: rgba(29, 29, 29, 0.7);
}

.product-info__text {
  font-size: 1.4rem;
  margin-bottom: 3rem;
  color: rgba(29, 29, 29, 0.7);
}

.detail-group {
  color: #222;
  margin-bottom: 4rem;
}

.detail-group__span {
  font-size: 1.1rem;
  text-transform: uppercase;
}

.detail-group__size {
  width: 12.5rem;
  font-size: 1.1rem;
  letter-spacing: 0.1rem;
  padding: 1.1rem 2rem;
  margin-top: 0.5rem;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border-radius: 0;
  border: 0;
  outline: none;
  background: url(../img/down-arrow.png) 85%/7% no-repeat #f8f8f8;
}

.detail-group__quantity {
  width: 7.5rem;
  padding: 1.1rem 1.6rem;
  margin-top: 0.5rem;
  outline: none;
  border: 0;
  background-color: #f8f8f8;
}

.clothing-item-flex {
  height: 100%;
  background-color: #fff;
  display: flex;
}

.clothing-item-flex__img-wrapper {
  min-width: 60%;
  margin: 1.5rem;
  overflow: hidden;
}

.clothing-item-flex__img-wrapper--no-margin {
  margin: 0;
}

.clothing-item-flex__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.btn {
  display: inline-block;
  font-size: 1.2rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.2rem;
  padding: 1.2rem 2.6rem;
  border: 2px solid #fff;
  color: #fff;
  text-decoration: none;
  outline: none;
  transition: all 250ms ease-in-out;
}
.btn:hover {
  color: #222;
  background-color: #fff;
}
.btn--form {
  color: #222;
  border: 2px solid black;
  font-weight: 400;
  padding: 1.4rem 3.5rem;
  cursor: pointer;
}
.btn--form:hover {
  color: #fff;
  background-color: #222;
}
.btn--form--shop {
  padding: 2.3rem 3.4rem;
}

.btn-view {
  display: block;
  width: 10.5rem;
  margin-top: 4rem;
  font-size: 1.4rem;
  letter-spacing: 0.1rem;
  text-decoration: none;
  color: rgba(61, 61, 61, 0.6);
  border-bottom: 1px solid rgba(162, 162, 162, 0.7);
}

.popup-btn {
  width: 3rem;
  height: 3rem;
  position: absolute;
  top: 50%;
  right: 9%;
  cursor: pointer;
}
.popup-btn--prev {
  transform: rotate(180deg);
  left: 9%;
}
.popup-btn__icon {
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="products-container container">

  <a href="https://www.pinterest.com/pin/449304500297815470/" class="shop-item">
    <img src="https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d4f67e4b00b2c2a29ab00/1443712877696/lauren-winter-haori-jacket_0250.jpg?format=750w" alt="Clothing item" class="lazy shop-item__img" id="HaoriJacketImg">
    <div class="quickview">
      <span class="quickview__icon" id="Haori Jacket">Quick View</span>
      <span class="quickview__info">Haori jacket<br><span class="quickview__info--price" id="HaoriJacketPrice">$210.00</span></span>
    </div>
  </a>
  
  <a href="https://www.pinterest.com/pin/ASQyu4IhVADMC-1ZPfodm2IgJJD-Q2dKvL8t-bQZv-H5rfuxCX52s94/" class="shop-item">
          <img src="https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d5226e4b0e3eb29871ecf/1443713579307/lauren-winter-swing-dress_0183.jpg?format=2500w" alt="Clothing item" class="lazy shop-item__img" id="SwingDressImg">
          <div class="quickview">
            <span class="quickview__icon" id="Swing Dress">Quick View</span>
            <span class="quickview__info">Swing dress<br><span class="quickview__info--price" id="SwingDressPrice">$218.00</span></span>
          </div>
        </a>
        
        <a href="#" class="shop-item">
          <img src="https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d4fd9e4b03b1013fd1f56/1443712996946/lauren-winter-lounge-jumpsuit-black_0284.jpg?format=750w" alt="Clothing item" class="lazy shop-item__img" id="LoungeJumpsuitImg">
          <div class="quickview">
            <span class="quickview__icon" id="Lounge Jumpsuit">Quick View</span>
            <span class="quickview__info">Lounge Jumpsuit<br><span class="quickview__info--price" id="LoungeJumpsuitPrice">$298.00</span></span>
          </div>
        </a>
        
        <a href="#" class="shop-item">
          <img src="https://static1.squarespace.com/static/560c458be4b0af26f729d191/560c5de0e4b083d9c365515f/560d542ae4b088b5adb66691/1443714094740/ulihu-blue-linen-crop-top_0320.jpg?format=750w" alt="Clothing item" class="lazy shop-item__img" id="LinenTopImg">
          <div class="quickview">
            <span class="quickview__icon" id="Linen Top">Quick View</span>
            <span class="quickview__info">Linen top<br><span class="quickview__info--price" id="LinenTopPrice">$125.00</span></span>
          </div>
        </a>
</section>

<!-- Popup Shop Item -->

<div class="overlay">
  <div class="popup-btn popup-btn--prev">
    <img src="https://image.flaticon.com/icons/svg/60/60758.svg" alt="Previous button" class="popup-btn__icon">
  </div>
  <div class="popup-item">
    <div class="clothing-item-flex">
      <div class="clothing-item-flex__img-wrapper">
        <img src="" alt="Clothing item" class="clothing-item-flex__img zoom-normal" id="clothingImg">
      </div>
      <div class="product-info">
        <h2 class="heading-secondary" id="clothingName"></h2>
        <span class="product-info__price" id="clothingPrice"></span>
        <p class="product-info__text">Sed ornare tellus non lectus blandit faucibus. Curabitur convallis nibh ut libero lobortis ullamcorper. Aliquam ornare risus in orci iaculis egestas. Vivamus varius ipsum eu leo ...</p>
        <div class="detail-group">
          <p class="detail-group__span">Size:</p>
          <select class="detail-group__size">
            <option value="">Select Size</option>
            <option value="0">0</option>
            <option value="2">2</option>
            <option value="4">4</option>
            <option value="6">6</option>
            <option value="8">8</option>
          </select>
        </div>
        <div class="detail-group">
          <p class="detail-group__span">Quantity:</p>
          <input class="detail-group__quantity" max="9999" min="1" value="1" type="number">
        </div>
        <button type="button" class="btn btn--form btn--form--shop">Add to cart</button>
        <a href="" class="btn-view">View Full Item</a>
      </div>
    </div>
    <span class="popup__close-icon-clothing" id="closeIcon">&times;</span>
  </div>
  <div class="popup-btn">
    <img src="https://image.flaticon.com/icons/svg/60/60758.svg" alt="Next button" class="popup-btn__icon">
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

再次查看您的示例后,我看到了问题所在。通常,您的quickview__icon跨度不会是<a />的子级,相反,它可以是例如同级,并且您可以将其绝对定位在shop-item上,这样可以避免尝试完全重定向。

但是,为了使您的代码几乎可以按原样工作,您可以这样做:

$('.quickview__icon').click(function(event) {
    event.preventDefault();

这将阻止<a />尝试重定向页面。

为将来参考,请阅读X-Y problem,并尝试查看您的问题为何属于该类别。