【发布时间】:2019-05-15 21:00:46
【问题描述】:
我正在使用一个名为 dropcaps.js 的 jQuery 库来定位 <p> 标记的第一个字母,并将其包装在 span 中。这按预期工作,但是<p> 的其余部分已剥离所有标签,例如<a>、<strong>、<em> 等。有没有办法编辑以下内容以保留这些格式标签?
// Init dropcaps
// Dropcaps
$(document).ready(function() {
// Isolate the first letter of paragraphs and place within <span>
$('p.has-drop-cap').each(function() {
var text = $.trim($(this).text());
var firstLetter = text.substr(0,1);
$(this).html('<span class="initial-letter">' + firstLetter + '</span>' + text.slice(1));
});
// Init dropcap.js and target <span>
var dropcaps = document.querySelectorAll(".initial-letter");
// Set dropcap number of rows to fit and baseline position
window.Dropcap.layout(dropcaps, 5, 5);
});
【问题讨论】: