javascript - 如何同时用 html 元素替换粗体、下划线等的文本?
问题描述
我在这里问一个先前的问题。大胆风格的解决方案是:
var a;
var b;
$(document).ready(function() {
$("#convertBtn").on("click", function() {
$('#questionDisplay').text($("#questionEdit").val());
a = $("#questionDisplay").text();
$('#questionDisplay').html(a.split("**").join('</b>'));
b = a.split("**").join('</b>');
$('#questionDisplay').html(b.split('*').join('<b>'));
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="questionEdit" rows="5" style="width: 500px;">The user types here many *bold** and *secondBold** texts. The convert button should change the stars to html tags.</textarea>
<button id="convertBtn">Convert</button>
<p>Display: </p>
<p id="questionDisplay"></p>
现在,我想添加下划线样式的功能,将来可能还会添加更多样式。问题是下划线文本的第二个代码删除了第一个代码中所做的事情,如下所示:
var a;
var b;
$(document).ready(function() {
$("#convertBtn").on("click", function() {
$('#questionDisplay').text($("#questionEdit").val());
a = $("#questionDisplay").text();
$('#questionDisplay').html(a.split("**").join('</b>'));
b = a.split("**").join('</b>');
$('#questionDisplay').html(b.split('*').join('<b>'));
a = $("#questionDisplay").text();
$('#questionDisplay').html(a.split("__").join('</u>'));
b = a.split("__").join('</u>');
$('#questionDisplay').html(b.split('_').join('<u>'));
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="questionEdit" rows="5" style="width: 500px;">Now the user types here *bold** with _underlined__ texts. But the code of underlined removes the work done by the code of bold!</textarea>
<button id="convertBtn">Convert</button>
<p>Display: </p>
<p id="questionDisplay"></p>
那么,这里的解决方案是什么?
解决方案
在下划线过程 whena
被赋予 的值之前text
,它不采用tags
输入的用于使文本变为粗体的。所以分配a
给
a = $("#questionDisplay").html();
代替
a = $("#questionDisplay").text();
这将确保也采用<b>
元素,以便两者可以一起工作。
var a;
var b;
$(document).ready(function() {
$("#convertBtn").on("click", function() {
$('#questionDisplay').text($("#questionEdit").val());
a = $("#questionDisplay").text();
$('#questionDisplay').html(a.split("**").join('</b>'));
b = a.split("**").join('</b>');
$('#questionDisplay').html(b.split('*').join('<b>'));
a = $("#questionDisplay").html();
$('#questionDisplay').html(a.split("__").join('</u>'));
b = a.split("__").join('</u>');
$('#questionDisplay').html(b.split('_').join('<u>'));
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="questionEdit" rows="5" style="width: 500px;">Now the user types here *bold** with _underlined__ texts. But the code of underlined removes the code of bold!</textarea>
<button id="convertBtn">Convert</button>
<p>Display: </p>
<p id="questionDisplay"></p>
推荐阅读
- macos - 在 Macos 应用程序中从 swift 登录 - 它在哪里?
- c# - C# Json 对模型的响应
- javascript - 嗨,我有一个手动工作的自动幻灯片。我的问题是我希望在您按下 btns 时重置计时器(用于自动幻灯片放映)
- android - 无法解析配置“:classpath”的所有工件。找不到 protos-27.2.2.jar
- python - 如何用Qpython打开手机手电筒?
- rust - 在塔层中检索请求主体以签署 GRPC 请求
- android - Android 11 no longer able to get album by id
- kotlin - Val-property cannot override var-property
- mongodb - 聚合整个集合,查找数组之间的重复元素 - 然后打印数组/json
- json - 如何将此 JSON 字符串转换为结构?