<span class="locationtitle">
<a href="#">Test Text</a><br>
6:00 p.m. - 12:00 a.m.<br>
Repeat on the first Thursday
</span>
jQuery を使用して最後の文を削除する方法を考え出そうとしています。したがって、2番目以降のすべて<br>
。その文のテキストは潜在的に何でもある可能性があるため、 aを実行しても機能しfind()
ません。
<span class="locationtitle">
<a href="#">Test Text</a><br>
6:00 p.m. - 12:00 a.m.<br>
Repeat on the first Thursday
</span>
jQuery を使用して最後の文を削除する方法を考え出そうとしています。したがって、2番目以降のすべて<br>
。その文のテキストは潜在的に何でもある可能性があるため、 aを実行しても機能しfind()
ません。
あなたはこれを行うことができます:
$('.locationtitle').html(function(_,html) {
return html.split(/<br\s*\/?>/i).slice(0,-1).join('<br>')
});
これは、 を書く他の方法をサポートします<br>
。例えば<BR/>
、
いくつかの説明:
Assuming that last sentence doesn't contain any HTML, a mixture of vanilla JS and jQuery may work best here:
var s = $('.locationtitle')[0].childNodes; // HTML and text nodes
var last = s[s.length-1]; // selects the last node
$(last).wrap('<span>').parent().hide(); // can only hide HTML elements