jquery - jquery 中的 .has()

标签 jquery

我正在尝试了解 jquery .has()。我尝试使用它来查找具有 class="a"的 li 项目并设置其边框。

<script type=text/javascript>
$("document").ready( function(){
    $("li").has(".a").css("border", "3px solid red");
});

<ul id="list1">
    <li class="a">item 1</li>
    <li class="a">item 2</li>
    <li class="b">item 3</li>
    <li class="b">item 4</li>
</ul>

最佳答案

.has() 查找与提供的选择器匹配的后代元素。来自 the API docs :

.has( selector )

Description: Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.

你想做的事情要简单得多!

$("li.a").css("border", "3px solid red");

关于jquery - jquery 中的 .has(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6021751/

相关文章:

jquery - 当 div 位于窗口顶部时显示菜单 - jquery

javascript - 如何将代码隐藏信息传递到 JavaScript 函数中

jquery - Div slideup 并在其位置显示另一个 div

JQuery 使用each() 获取准确的元素高度

javascript - FabricJS 对象与 Canvas 调整大小无关

javascript - 在 Rails 中使用 jquery .load()

php - 使用 PHP/Ajax/Jquery 显示根据内容格式化的动态帖子

javascript - 幻影 : take screenshot of the current page?

javascript - 从javascript中的数组中删除单击的项目

javascript - 如何在多个选择中保留现有文件选择