热门关键字:
jquery > jquery教程 > jquery教程 > jQueryparents()方法

jQueryparents()方法

262
作者:管理员
发布时间:2021/1/29 13:56:49
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=3613
jQueryparents()方法用于获取所选元素的祖先。


下面的例子将添加边框周围的所有祖先元素<li>这是<ul>,<div>,<body>和<html>元素。


例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery parents() Demo</title>
<style>
    *{
        margin: 10px;
    }
    .frame{
        border: 2px solid green;
    }        
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("li").parents().addClass("frame");
});
</script>
</head>
<body>
    <div class="container">
        <h1>Hello World</h1>
        <p>This is a <em>simple paragraph</em>.</p>
        <ul>
            <li>Item One</li>
            <li>Item Two</li>
        </ul>
    </div>
</body>
</html>
您可以选择在parents()方法中包含一个或多个选择器作为参数,以过滤对祖先的搜索。以下示例将<li>在<div>元素的所有祖先周围应用边框。




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:jQuery
友荐云推荐