javascript - 使用html实体的Javascript替换()不起作用
问题描述
我有以下脚本,它成功地将 < 和 > 替换为下面指示的代码。这里的想法是,如果用户希望“Bold me”在他们的博客上以粗体显示,他们会输入文本框。
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('<', '<span class="bold">'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('>', '</span>'));
});
问题来自其他 html 实体。我将简单地举个例子。我想用段落标签替换 [ html 实体,但此脚本中的所有行都不起作用。我尝试记录与“[”字符相关的每个代码。
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('[', '<p>'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('[', '<p>'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('[', '<p>'));
});
$('.blogbody').each(function() {
var string = $(this).html();
$(this).html(string.replace('[', '<p>'));
});
对此的任何想法将不胜感激!谢谢!
解决方案
该字符'['
不是字符实体,因此未编码。只需将其直接传递给replace
:
string.replace('[' , '<p>')
推荐阅读
- android - Android Q - 在执行程序上执行 AsyncTask 时出现 StackOverflowError
- android - 使用辅助功能服务关闭其他应用程序的权限
- ios - iOS App Store:如何共享所选应用商店/地区的分析数据?
- vue.js - 你如何等待 vue.js 创建的钩子?
- python - 计算数据框中列内的列中的每个值
- c# - 如何正确使用 HttpClient 类与代理和 cookie 更改 PER 请求(使用多线程)
- hyperledger-fabric - 启动结构服务器时 ./startFabric.s 显示错误
- docker - 在 docker-compose 文件中使用 buildx build linux/arm64
- angular - 如何在 Angular 应用程序中取消订阅 http post 请求?
- c# - 是否可以让 gRPC 服务器也充当客户端?C#