开发者

JQuery split by selector

I would like to know if there is a metho开发者_运维知识库d 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.


Sounds like your HTML should look like this:

<html>
    <head></head>
    <body>
        <div id="block1">
            <h1>Heading</h1>
            <p>Some text</p>
            <p>More text</p>
        </div>
        <div id="block2">
            <h2>Sub Heading</h2>
            <p>Some other text</p>
        </div>
        <div id="block3">
            <p>Yet more text</p>
            <p>and another</p>
        </div>
    </body>
</html>

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.


Depending on what you're trying to do (because I can't see this as being all that useful IRL), you could use split.

var html = $("body").html().split(/<br[^>]?>/gi);
for(var i = 0; i < html.length; i++){
    // $(html[i]) etc.
}

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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜