jQuery:在提示词后创建 div 容器的快捷方式
我正在为博客主题创建一个快捷方式,我想在使用提示词后在元素周围生成一个 div 容器。例如,我的博客条目是这样的:
<div class="entry">
<p>First Paragraph</p>
<p>[box]</p>
<p>Second Paragraph</p>
<p>Third Paragraph</p>
</div> <!-- .entry -->
我希望通过一些 jQuery 魔法,它可以变成这样:
<div class="entry">
<p>First Paragraph</p>
<div class="box">
<p>Second Paragraph</p>
<p>Third Paragraph</p>
</div> <!-- .box -->
</div> <!-- .entry -->
还有一条规则:当我创建一个容器框时,我知道我总是会在关闭 div.entry 之前生成它。我希望这个限制将使编写 jQuery 规则变得更容易。例如,我永远不会希望标记看起来像这样,其中有内容在 div.box 容器中进行:
<!-- I will never mark it up this way -->
<div class="entry">
<p>First Paragraph</p>
<div class="box">
<p>Second Paragraph</p>
</div> <!-- .box -->
<p>Third paragraph</p>
</div> <!-- .entry -->
I'm creating a shortcut for a blog theme where I want to generate a div container around elements after using a cue word. For example, my blog entry would like this:
<div class="entry">
<p>First Paragraph</p>
<p>[box]</p>
<p>Second Paragraph</p>
<p>Third Paragraph</p>
</div> <!-- .entry -->
I'm hoping with some jQuery magic it could turn into this:
<div class="entry">
<p>First Paragraph</p>
<div class="box">
<p>Second Paragraph</p>
<p>Third Paragraph</p>
</div> <!-- .box -->
</div> <!-- .entry -->
One more rule: When I create a container box, I know I will always generate it before the closing div.entry. I'm hoping this restriction will make it easier to write the rules for jQuery. For example, I will never want the markup to look like this where there is content proceeding the div.box container:
<!-- I will never mark it up this way -->
<div class="entry">
<p>First Paragraph</p>
<div class="box">
<p>Second Paragraph</p>
</div> <!-- .box -->
<p>Third paragraph</p>
</div> <!-- .entry -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你最好的选择是 jQuery :contains() 选择器。
有了它,你可以做这样的事情(注意:它匹配 HTML 中包含 [box] 的任何段落,也许你需要转义括号):
顺便说一句。接受答案并证明您已经付出了努力
问题将使其更有可能获得有用的答复。
I think your best bet is the jQuery :contains() selector.
With it you could do things like this (note: it matches any paragraph that has [box] in its HTML and maybe you need to escape the brackets):
And btw. accepting answers and proving that you already put effort in your
problem will make it much more likely to get a helpful reply.
它将是这样的:
It will be something like this:
在此处查看以下示例。
您可以找到
index()
[box] 段落,然后wrapAll()
使用:gt()
获取其后面的所有段落:See an example of the following here.
You can find the
index()
of the [box] paragraph and thenwrapAll()
the<p>
after using:gt()
to get all the parapgraphs following it:感谢大家的帮助!它还帮助我想出了另一个对我有用的策略:
它的作用是创建两个单独的框:1.元素在[box]之前,2.元素在[box]之前
Thanks everyone for your help! It helped me also come up with another strategy that worked for me as well:
What this does is create two separate boxes: 1. elements preceding [box], 2. elements proceeding [box]