选择具有href属性而不以字符串开头的元素

时间:2019-02-11 10:36:40

标签: javascript jquery

$('.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');

1 个答案:

答案 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>