JQuery 按选择器拆分
我想知道是否有一种方法可以将 jQuery 对象拆分为多个部分(基于选择器),但不使用基本的 javascript split();
方法,该方法仅适用于细绳。
我希望能够做类似的事情: $("body").splitBySelector("br").each();
等等...
<html>
<head></head>
<body>
<h1>Heading</h1>
<p>Some text</p>
<p>More text</p>
<br />
<h2>Sub Heading</h2>
<p>Some other text</p>
<br class="atrivialexample"/>
<p>Yet more text</p>
<p>and another</p>
</body>
</html>
但是正如你所看到的,如果我是使用 split();
函数我将无法解释标记中可能存在或不存在的任何类或 id 或任何其他属性。而且我不会返回 jquery 对象,因此我可以继续链接。
解决方案越简单越好。
I would like to know if there is a method for splitting a jQuery object into multiple parts (based on a selector), but not using the base javascript split();
method, which would only work on a string.
I'd want to be able to do something like: $("body").splitBySelector("br").each();
etc etc...
<html>
<head></head>
<body>
<h1>Heading</h1>
<p>Some text</p>
<p>More text</p>
<br />
<h2>Sub Heading</h2>
<p>Some other text</p>
<br class="atrivialexample"/>
<p>Yet more text</p>
<p>and another</p>
</body>
</html>
But as you can see if I were to use the split();
function I would not be able to account for any classes or ids or any other attributes that may or may not be present in the markup. And I would not be returned jquery objects,so I could continue chaining.
The simpler the solution the better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您的 HTML 应该如下所示:
然后,您就知道该怎么做了。
否则,如果您坚持,
.nextUntil
可能对您有用。文档页面上的示例看起来与您所询问的非常相似。Sounds like your HTML should look like this:
And then, well, you know what to do.
Otherwise, if you insist,
.nextUntil
may be of use to you. The example on the documentation page there looks very similar to what you're asking about.根据您想要执行的操作(因为我认为这在现实生活中不太有用),您可以使用 split。
它并不是真正有用,因为在 for 循环中创建的 jQuery 对象除了是一个副本之外与页面上的内容没有任何关系......
Depending on what you're trying to do (because I can't see this as being all that useful IRL), you could use split.
It's not real useful because the jQuery object created there in the for loop doesn't have any relationship to what's on the page aside from being a copy...