【问题标题】:count elements with specific data using ES6 concatenation [duplicate]使用 ES6 连接计算具有特定数据的元素
【发布时间】:2019-07-03 17:48:32
【问题描述】:

尝试使用ecmascript 6 变量连接。

在控制台中期望 exists 但出现错误。请帮忙:

let path = 'abc';
if($('.title[data-path=${path}]').length > 0){
  console.log('exists');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class='title' data-path = 'abc'>lorem</div>
<div class='title' data-path = 'def'>ipsum</div>
<div class='title' data-path = 'abc'>lorem</div>

【问题讨论】:

  • 使用模板文字而不是''

标签: javascript jquery


【解决方案1】:

改用串联

let path = 'abc';
if($(`.title[data-path=${path}]`).length > 0){
  console.log('exists');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class='title' data-path = 'abc'>lorem</div>
<div class='title' data-path = 'def'>ipsum</div>
<div class='title' data-path = 'abc'>lorem</div>

【讨论】:

  • 现在检查编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 2018-11-21
相关资源
最近更新 更多