Javascript 包装所有®;HTML文档中的字符(<;sup>®</sup>;使用jquery
我有一份贯穿整个文档的多个®字符的文档。我需要让它们都呈现在上标中。我正在尝试,但没有运气:Javascript 包装所有®;HTML文档中的字符(<;sup>®</sup>;使用jquery,javascript,jquery,encoding,superscript,Javascript,Jquery,Encoding,Superscript,我有一份贯穿整个文档的多个®字符的文档。我需要让它们都呈现在上标中。我正在尝试,但没有运气: $('body:contains("®")').contents().each(function () { if (this.nodeType == 1) { //Look for only element Nodes $(this).html(function (_, oldValue) { return oldValue.replace(/®/g, "
$('body:contains("®")').contents().each(function () {
if (this.nodeType == 1) { //Look for only element Nodes
$(this).html(function (_, oldValue) {
return oldValue.replace(/®/g, "<sup>$&</sup>")
})
}
});
$('body:contains(“”)).contents().each(函数(){
如果(this.nodeType==1){//只查找元素节点
$(this).html(函数(\ux,oldValue){
返回旧值。替换(/®/g,“$&”)
})
}
});
您可以使用基本替换
$("body").html($("body").html().replace(/\u00AE/g, "<sup>®</sup>"))
$(“body”).html($(“body”).html().replace(/\u00AE/g,“®;”)
我不确定这对于生产来说是否足够性能或健壮,但在我有限的测试中它似乎起作用:
$(function () {
var html = $('body').html();
html = html.replace(/®/g, "<sup>®</sup>");
$("body").html(html);
});
$(函数(){
var html=$('body').html();
html=html.replace(/?/g,“?”);
$(“body”).html(html);
});
JSFiddle:
基本上,将正文的HTML作为字符串抓取,执行正则表达式搜索/替换,并用新字符串替换正文的HTML。这取决于字符如何放置到HTML中,但可能就这么简单-
$("body").children().each(function() {
$(this).html($(this).html().replace(/\®/g,"<sup>®</sup>"));
});
$(“body”).children().each(function()){
$(this.html($(this.html().replace(/\®;/g,“®;”)));
});