jQuery:在提示词后创建 div 容器的快捷方式

发布于 2024-10-16 10:05:48 字数 1004 浏览 2 评论 0原文

我正在为博客主题创建一个快捷方式,我想在使用提示词后在元素周围生成一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江湖正好 2024-10-23 10:05:48

我认为你最好的选择是 jQuery :contains() 选择器。
有了它,你可以做这样的事情(注意:它匹配 HTML 中包含 [box] 的任何段落,也许你需要转义括号):

$("p:contains('[box]')").wrap($('<div>').addClass('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):

$("p:contains('[box]')").wrap($('<div>').addClass('box'));

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.

傲性难收 2024-10-23 10:05:48

它将是这样的:

$("div.entry").append(
  $("<div>").addClass("box").append("p:contains([box])+*");
);
$("p:contains([box])").remove();

It will be something like this:

$("div.entry").append(
  $("<div>").addClass("box").append("p:contains([box])+*");
);
$("p:contains([box])").remove();
尾戒 2024-10-23 10:05:48

在此处查看以下示例。

您可以找到 index() [box] 段落,然后 wrapAll() 使用 :gt() 获取其后面的所有段落:

var boxAt;

$('p').each(function(){
    var $t = $(this);
    if ($t.html() === '[box]') {
        boxAt = $t.index();
        $t.remove();
    }
});

$('p:gt(' + (boxAt - 1) + ')').wrapAll('<div class="box">');

See an example of the following here.

You can find the index() of the [box] paragraph and then wrapAll() the <p> after using :gt() to get all the parapgraphs following it:

var boxAt;

$('p').each(function(){
    var $t = $(this);
    if ($t.html() === '[box]') {
        boxAt = $t.index();
        $t.remove();
    }
});

$('p:gt(' + (boxAt - 1) + ')').wrapAll('<div class="box">');
淡淡的优雅 2024-10-23 10:05:48

感谢大家的帮助!它还帮助我想出了另一个对我有用的策略:

$('.entry p:contains([box])').each( function() {
    $(this).prevAll().wrapAll('<div class="lefty">');
});

$('.entry p:contains([box])').each( function() {
    $(this).nextAll().wrapAll('<div class="righty">');
});
$("p:contains([box])").remove();

它的作用是创建两个单独的框:1.元素在[box]之前,2.元素在[box]之前

Thanks everyone for your help! It helped me also come up with another strategy that worked for me as well:

$('.entry p:contains([box])').each( function() {
    $(this).prevAll().wrapAll('<div class="lefty">');
});

$('.entry p:contains([box])').each( function() {
    $(this).nextAll().wrapAll('<div class="righty">');
});
$("p:contains([box])").remove();

What this does is create two separate boxes: 1. elements preceding [box], 2. elements proceeding [box]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文