いくつかの段落内の部分文字列(角かっこで囲まれた文字数)を選択し、クラスが適用された<span>
タグで各文字列をラップしようとしています。char_count
1つのHTMLとCSSは次のとおりです。
var select_p = $('div#promo_area div.featured_box p');
select_p.each(function() {
var first_index = $(this).html().indexOf('[');
var last_index = $(this).html().indexOf(']') + 1;
var selected_text = $(this).html().substring(first_index, last_index);
selected_text.wrap('<span class="char_count" />');
});
span.char_count {
padding-top: 0;
color: #ff6600 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="promo_area">
<h3>Featured Stories</h3>
<div class="featured_box">
<h4><a href="/give">Senectus et Netus</a></h4>
<div>
<a href="/give"><img width="207" height="139" src="http://myrussreid.com/files/2011/06/ffffff1395-207x139.jpg" class="attachment-wds_home_image wp-post-image" alt="ffffff139" title="ffffff139" /></a>
</div>
<p>Pellentesque habitant morbi tristique senectus et netus. [100 characters w/spaces]</p>
<a href="/give">Please Give</a>
</div>
<!-- end .featured_box -->
</div>
行までは機能しているようです.wrap
—で正しい部分文字列を取得しますselected_text
。ラップ自体は機能しません。私が間違っているのはどんな愚かなことですか?それとも愚かな大きなことですか?
これが私のフィドルです。