尝试使用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>
答案 0 :(得分:0)
改为使用串联
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>