当这些项目由 <br>
分隔时,为什么 Jquery 的 .next() 函数不返回下一个选定的项目标签?
以下是从 JQueryUI 网站演示区获取的一些代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Button - Icons</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "button:first" ).button({
icons: {
primary: "ui-icon-locked"
},
text: false
}).next().button({
icons: {
primary: "ui-icon-locked"
}
}).next().button({
icons: {
primary: "ui-icon-gear",
secondary: "ui-icon-triangle-1-s"
}
}).next().button({
icons: {
primary: "ui-icon-gear",
secondary: "ui-icon-triangle-1-s"
},
text: false
});
});
</script>
</head>
<body>
<button>Button with icon only</button>
<button>Button with icon on the left</button>
<button>Button with two icons</button>
<button>Button with two icons and no text</button>
</body>
</html>
这会导致如下显示:
但是如果我插入 <br>
标记在第一个元素之后,然后跳过下一个 .button() 调用,导致以下显示:
第二个 .next().button( ... "ui-icon-locked"...) 函数似乎被跳过,其余的 .next().button() 函数被关闭-一个。
我可以通过使用 ID、创建按钮集并设置其样式来解决此问题,我确信还有其他方法,但是 JQuery 选择和 <br>
有什么问题吗?我在这里缺少标签吗?
最佳答案
.next()
始终返回下一个元素。向该函数传递参数只是一个“条件”。如果不满足条件,jQuery 对象将为空。
您可以将 .nextAll()
与 :first
结合使用来解决此问题:
$('button:first').button(...).nextAll('button:first') //and on and on
关于jquery .next() 跳过由 <br> 标签分隔的选定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989095/