需要使用 javascript 将选定的文本设置为粗体/斜体/下划线,并保存并保存。使用 c# 检索相同的内容
我需要使用 javascript 将文本框的选定文本设为粗体/斜体/下划线。为此,我正在使用以下代码。
<img src="~/images/Bold" alt="Bold" onclick="changeFont('TextBox1','b');" />
<img src="~/images/Italic" alt="Italic" onclick="changeFont('TextBox1','i');" />
<img src="~/images/Underline" alt="Underline" onclick="changeFont('TextBox1','u');" />
<script type="text/javascript" language="javascript">
function changeFont(txt, change) {
if (change == 'b') {
if (document.getElementById(txt).style.fontWeight == 'bold')
document.getElementById(txt).style.fontWeight = 'normal';
else
document.getElementById(txt).style.fontWeight = 'bold';
}
else if (change == 'i') {
if (document.getElementById(txt).style.fontStyle == 'italic')
document.getElementById(txt).style.fontStyle = 'normal';
else
document.getElementById(txt).style.fontStyle = 'italic';
}
else {
if (document.getElementById(txt).style.textDecoration == 'underline')
document.getElementById(txt).style.textDecoration = 'none';
else
document.getElementById(txt).style.textDecoration = 'underline';
}
}
</script>
但这里的问题是,当我单击粗体图像时,它会使整个文本变为粗体,而不是所选文本。它也不适用于其他两个图像。
在保存文本框的文本时,即使在尝试后我也无法获取包括 html 标签的文本,
document.getElementById('TextBox1').innerHTML;
我只能获取文本框的值。
有没有办法使用 javascript 或 C# 保存和检索相同的内容
提前致谢 SC
I need to make selected text of textbox bold/italic/underline using javascript. For that i am using the following code.
<img src="~/images/Bold" alt="Bold" onclick="changeFont('TextBox1','b');" />
<img src="~/images/Italic" alt="Italic" onclick="changeFont('TextBox1','i');" />
<img src="~/images/Underline" alt="Underline" onclick="changeFont('TextBox1','u');" />
<script type="text/javascript" language="javascript">
function changeFont(txt, change) {
if (change == 'b') {
if (document.getElementById(txt).style.fontWeight == 'bold')
document.getElementById(txt).style.fontWeight = 'normal';
else
document.getElementById(txt).style.fontWeight = 'bold';
}
else if (change == 'i') {
if (document.getElementById(txt).style.fontStyle == 'italic')
document.getElementById(txt).style.fontStyle = 'normal';
else
document.getElementById(txt).style.fontStyle = 'italic';
}
else {
if (document.getElementById(txt).style.textDecoration == 'underline')
document.getElementById(txt).style.textDecoration = 'none';
else
document.getElementById(txt).style.textDecoration = 'underline';
}
}
</script>
But the issue here is, when i click on bold image its making the whole text into bold but not the selected text. It´s not working for the other two images either.
While saving the text of textbox I am unable to get the text including html tags even after trying with
document.getElementById('TextBox1').innerHTML;
I am able to get only the value of textbox.
Is there any way to save and retrieve the same using javascript or C#
Thanks in advance
SC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个回答您有关获取突出显示文本的问题的问题
如何获取文本区域中选定的文本?
关于将选定文本设为粗体当您将其打印到页面上时,您需要使用 html 标签或类似 bbcode 的内容并将其解析为 html。
编辑: 这是一个显示 jquery 插件“fieldselection”的页面编辑
2:这是我如何做到这一点的示例: jsfiddle 链接
HTML:
javascript (jquery) 代码:
Here is a question that answers your problem about getting the highlighting text
How to get selected text in textarea?
About making the selected text bold you would need to use html tags or something like bbcode and parse it to html when you print it on to a page.
EDIT: Here is a page that shows the jquery plugin "fieldselection" in action.
EDIT 2: Here is an example of how I would've done this: jsfiddle link
The HTML:
The javascript (jquery) code:
document.execCommand("粗体", false, null);
这是对我有用的最简单的技术
在所有浏览器中...
document.execCommand("bold", false, null);
this is Simplest techinique which worked for me
in all browsers ...