jQuery api .next()

来源:互联网 发布:mac pro怎么安装双系统 编辑:程序博客网 时间:2024/06/16 13:08

.next()

描述: 取得匹配的元素集合中每一个元素紧邻的后面同辈元素的元素集合。如果提供一个选择器,那么只有紧跟着的兄弟元素满足选择器时,才会返回此元素。

.next([selector])

selector
类型: Selector
一个字符串,其中包含一个选择器表达式针对匹配元素。

如果一个jQuery代表了一组DOM元素, .next()方法允许我们找遍元素集合中紧跟着这些元素的直接兄弟元素,并根据匹配的元素创建一个新的 jQuery 对象。

该方法还可以接受一个可选的选择器表达式,该选择器表达式可以是任何可传给 $() 函数的选择器表达式。如果每个元素的直接兄弟元素满足所提供的选择器,那么它会保存在新生成的 jQuery 对象中,否则,不会包含该元素

例子:

Example: 查找每个紧随着被禁用的按钮的元素,并将其文本变为 "this button is disabled"。

<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title><script src="jquery-1.10.2.js"></script><style>   span { color:blue; font-weight:bold; }  button { width:100px; }  </style></head><body><div><button disabled="disabled">第一个</button> - <span></span></div>    <div><button>第二个</button> - <span></span></div>        <div><button disabled="disabled">第三个 </button> - <span></span></div>    <script>    $("button[disabled]").next().text("被禁用的按钮");    </script></body></html>

效果图:


Example: 查找每个紧随着段落的元素。只保留其中含有 "selected" 样式的元素。

<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title><script src="jquery-1.10.2.js"></script><style>   span { color:blue; font-weight:bold; }  button { width:100px; }  </style></head><body>        <p>第一个</p>     <p class="selected">第二个</p>    <div><span>第三个</span></div>    <script>    $("p").next(".selected").css("background","red");    </script></body></html>

效果图:


0 1