首页 > 解决方案 > Javascript - Div 隐藏/显示 - Safari 问题

问题描述

我有以下脚本,它在 Chrome 中运行良好,但在 Safari 中运行良好。

在 Safari 中,我收到以下错误:SyntaxError:字符串与预期的模式不匹配。

const btnList = document.querySelectorAll("a[id^=category]");
btnList.forEach(function(btn){
  btn.addEventListener("click", function(e){
    e.preventDefault();
    const id = this.id.replace('SeeMore', '')
    document.querySelectorAll('.' + id).forEach(el=>el.style.display = 'block')
  });
});
.category36, .category37 {
  margin: 5px;
  padding: .5rem;
  box-shadow: 2px 2px 5px rgba(0,0,0,0.03);
  border-radius: 4px;
  background: Skyblue;
  border-bottom: 1px solid #F9F2D6;
  border-right: 1px solid #F9F2D6;
}
<p>
  <a href="#" id="category36SeeMore">See more</a>
</p>

<div class="category36">
  test
</div>

<div class="category36" style="display:none;">
  test
</div>

<div class="category36" style="display:none;">
  test
</div>

<div class="category36" style="display:none;">
  test
</div>

<hr>

<p>
  <a href="#" id="category37SeeMore">See more</a>
</p>

<div class="category37">
  test
</div>

<div class="category37" style="display:none;">
  test
</div>

<div class="category37" style="display:none;">
  test
</div>

<div class="category37" style="display:none;">
  test
</div>

标签: javascript

解决方案


推荐阅读