将内容附加到包含特定文本的元素。

huangapple go评论47阅读模式
英文:

jquery append to element that contains specific text

问题

You can use the following jQuery code to insert HTML into the card-body that contains the text "card b" in the card-header:

$(document).ready(function() {
    // Find the card with the desired card-header text
    var cardB = $(".card-header:contains('card b')").closest(".card");
    
    // Insert your HTML content into the card-body of card b
    cardB.find(".card-body").html("Your HTML content goes here");
});

请注意,你需要将"Your HTML content goes here"替换为你要插入的实际HTML内容。

英文:

I have the following html which is a Bootstrap card with two nested cards.

<div id="myCard" class="card-body">
    <div class="card">
        <div class="card-header">
            card a
        </div>
        <div class="card-body">

        </div>
    </div>
    <div class="card">
        <div class="card-header">
            card b
        </div>
        <div class="card-body">

        </div>
    </div>
</div>

How can I insert html into the card-body that contains the text "card b" in the card-header using JQuery?

答案1

得分: 2

松散的遍历(如果元素被包装,更稳定):

$('.card-header:contains(card b)')
  .parents('.card')
  .find('.card-body')
  .text('插入的文本');

更严格的遍历:

$('.card-header:contains(card b)')
  .parent()
  .children('.card-body')
  .text('插入的文本');

请注意,这只是代码的一部分,如有其他需要,请告诉我。

英文:

Loose traversing (more stable if the elements get wrapped):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;.card-header:contains(card b)&#39;)
  .parents(&#39;.card&#39;)
  .find(&#39;.card-body&#39;)
  .text(&#39;INSERTED TEXT&#39;);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;myCard&quot; class=&quot;card-body&quot;&gt;
    &lt;div class=&quot;card&quot;&gt;
        &lt;div class=&quot;card-header&quot;&gt;
            card a
        &lt;/div&gt;
        &lt;div class=&quot;card-body&quot;&gt;

        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;card&quot;&gt;
        &lt;div class=&quot;card-header&quot;&gt;
            card b
        &lt;/div&gt;
        &lt;div class=&quot;card-body&quot;&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

More strict traversing:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;.card-header:contains(card b)&#39;)
  .parent()
  .children(&#39;.card-body&#39;)
  .text(&#39;INSERTED TEXT&#39;);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;myCard&quot; class=&quot;card-body&quot;&gt;
    &lt;div class=&quot;card&quot;&gt;
        &lt;div class=&quot;card-header&quot;&gt;
            card a
        &lt;/div&gt;
        &lt;div class=&quot;card-body&quot;&gt;

        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;card&quot;&gt;
        &lt;div class=&quot;card-header&quot;&gt;
            card b
        &lt;/div&gt;
        &lt;div class=&quot;card-body&quot;&gt;

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 07:25:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定