javascript - 如何让 Javascript 停止从展开/折叠栏中删除我的填充

标签 javascript jquery html css

看来我的 JS 正在删除 css 选择器 .header span 上的填充样式

我不是很确定为什么,或者如何让它停止,所以任何答案都会被大大应用。

下面是我当前的标记。

$(".header").click(function() {
  $header = $(this);
  //getting the next element
  $content = $header.next();
  //open up the content needed - toggle the slide- if visible, slide up, if not slidedown.
  $content.slideToggle(220, function() {
    //execute this after slideToggle is done
    //change text of header based on visibility of content div
    $header.text(function() {
      //change text based on condition
      return $content.is(":visible") ? "Show less" : "Show more";
    });
  });
});
body {
  margin: 0px 0px;
}

.ob_hero {
  background-image: url(https://s7d2.scene7.com/is/image/aeo/20171102_hpc_clearance_lg?scl=1&qlt=60);
  background-size: cover;
  background-position: center;
  position: relative;
  height: 100vh;
  width: 100vw;
}

.ob__herowrapper {
  position: absolute;
  top: 48%;
  text-align: center;
  width: 100%;
  color: #fff;
  -ms-transform: translate(0, -50%);
  /* IE 9 */
  -webkit-transform: translate(0, -50%);
  /* Safari */
  transform: translate(0, -50%);
}

.ob__herowrapper picture img {
  max-width: 80vw;
}

.btn_wrapper {
  position: relative;
  bottom: -50px;
  text-align: center;
  width: 100%;
  color: #fff;
  -ms-transform: translate(0, -50%);
  /* IE 9 */
  -webkit-transform: translate(0, -50%);
  /* Safari */
  transform: translate(0, -50%);
}

.herobtn_container {
  width: 80%;
  max-width: 500px;
  margin: 0 auto;
  position: relative;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.herobtn_wrapper {
  border: 2px solid transparent;
  flex-basis: 45%;
  flex-grow: 1;
}

.herobtn_1 {
  font-family: 'Source Sans Pro', sans-serif;
  font-size: 14px;
  background-color: #fff;
  color: #000;
  padding: 10px;
  text-align: center;
}

.expand {
  position: absolute;
  bottom: 0;
  width: 100vw;
  font-family: 'Source Sans Pro', sans-serif;
  font-weight: normal;
  font-size: 14px;
  background-color: #eee;
  z-index: 999999;
}

.expand div {
  width: 100%;
}

.expand .header {
  width: 100%;
  background-color: #000;
  color: #fff;
  cursor: pointer;
}

.header span {
  padding: 1em;
  display: block;
}

.expand .content {
  display: none;
}


/* Make Hero Buttons 100% on Mobile
    
    @media (max-width:50em) {
        .herobtn_container {
            position: relative;
            width: 100%;
        }
    }
    
    */


/* Tablet */

@media (min-width:30em) and (max-width:50em) {
  .ob_hero {
    background-image: url(https://s7d2.scene7.com/is/image/aeo/20171102_hpc_clearance_lg?scl=1&qlt=60);
  }
  .herobtn_wrapper {
    flex-basis: 45%;
  }
  .ob__herowrapper picture img {
    max-width: 70vw;
  }
}


/* Mobile */

@media (max-width:30em) {
  .ob_hero {
    background-image: url(https://s7d2.scene7.com/is/image/aeo/20171102_hpc_clearance_lg?scl=1&qlt=60);
  }
  .herobtn_wrapper {
    flex-basis: 45%;
  }
  .ob__herowrapper picture img {
    max-width: 70vw;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ob_hero">
  <div class="ob__herowrapper">
    <picture>
      <source media="(min-width: 50em)" srcset="http://svgshare.com/i/4H9.svg" width="100%" />
      <source media="(min-width: 0em)" srcset="http://svgshare.com/i/4J0.svg" width="100%" /> <img alt="ALT HERE" src="http://svgshare.com/i/4H9.svg" width="100%" /> </picture>
    <div class="btn_wrapper">
      <div class="herobtn_container">
        <div class="herobtn_wrapper">
          <div class="herobtn_1">SHOP WOMEN</div>
        </div>
        <div class="herobtn_wrapper">
          <div class="herobtn_1">SHOP MEN</div>
        </div>
        <div class="herobtn_wrapper">
          <div class="herobtn_1">SHOP GIRLS</div>
        </div>
        <div class="herobtn_wrapper">
          <div class="herobtn_1">SHOP BOYS</div>
        </div>
      </div>
    </div>
  </div>
  <div class="expand">
    <div class="header"><span>Show more</span> </div>
    <div class="content">
      <ul>
        <li>Valid In-Store & Online, Today Only</li>
        <li>Online Code: <strong>CLEARANCE15</strong></li>
        <li>Exclusions apply.</li>
      </ul>
    </div>
  </div>

我考虑过使用 CSS 转换,但我确信有一些 javascript 大师对这个问题有一个简单的修复。

谢谢!

最佳答案

它不是删除填充,而是删除整个 <span>元素。 $header<div class="header">你正在覆盖它的内容。使用 .find('span')访问子节点:

$header.find('span').text(function () {
    //change text based on condition
    return $content.is(":visible") ? "Show less" : "Show more";
});

关于javascript - 如何让 Javascript 停止从展开/折叠栏中删除我的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47760514/

相关文章:

javascript 在 joomla 3.0 中不起作用

javascript - 想要将延迟添加到 Keydown 事件

javascript - 我可以在 useMemo 中安全导航吗

javascript - 自定义 Chartjs 2.x 工具提示

javascript - 使用记录行进行 ajax 编程的有效方法

php - 如何从网页打印特定表格

javascript - 此代码在片段中有效,但在博客中无效

javascript - 如何在javascript中通过属性删除对象

html - 是否可以用@font-face 替换图标?

java - 使用 java/javascript 创建一个 HTML 跨度作为文本