$('.el[href^="view.php"]').css('background', 'gold');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class='el' href = 'view.php?id=323'>lorem</a>
<a class='el' href = 'view.php?id=525'>lorem</a>
<a class='el' href = 'index.php'>lorem</a>
<a class='el' href = 'about.php'>lorem</a>
以上方法工作正常,但现在我需要以href
开头的元素view.php
不是。
我尝试没有成功:
$('.el[href^!="view.php"]').css('background', 'gold');
答案 0 :(得分:5)
您可以在jQuery的Attribute Starts With Selector中使用not()
选择器:
$('.el').not('[href^="view.php"]').css('background', 'gold');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class='el' href = 'view.php?id=323'>lorem</a>
<a class='el' href = 'view.php?id=525'>lorem</a>
<a class='el' href = 'index.php'>lorem</a>
<a class='el' href = 'about.php'>lorem</a>