在我的表单中,我希望允许用户对文本进行格式设置并添加列表等等(基本的html功能,而不必知道html)。将格式化标签添加到文本框的值
使用粗体按钮为例:
<div class="goBold button">B</div>
<input type="text" name="txtMessage" />
<script>
$(".goBold").click(function() {
formatText("bold");
});
function formatText(formatType)
{
var input = $("#txtMessage");
var text = input.val();
var ss = input[0].selectionStart;
var se = input[0].selectionEnd;
if (formatType == "bold")
{
if (ss == se)
{
// there's no text in the textbox, so just write in the tags
input.val(text + "[b][/b]");
}
else
{
// surround the highlighted text with the tags
input.val(text.substr(0, ss) + "[b]" + text.substr(ss, se) + "[/b]" + text.substring(se, text.length));
}
}
}
</script>
虽然这一切工作,还有一个小问题:
让我们假设这个文本框我的文字值
的快速的红色狐狸跳过懒惰的棕色狗。这是一个使用字母
随着all 26 letters
强调,点击大胆的按钮,将包装的标签文字字样的所有26个字母的英语只有一句话,但它也将增加的第二个副本选择后存在的文本。
我不确定这是为什么,任何人都可以在这里提供任何见解?
TIA :)
很好的说明,但这只是一个错字,当我在写代码哈哈我会解决 – Ortund
那么,工程,谢谢:)只有,我想我需要改变默认(没有选择),以便它插入标签无论用户在文本框中的当前位置(闪烁的管道事情大声笑不知道它叫什么),虽然我'米不知道该怎么做,要么 – Ortund
HAH事实证明,它比预期更容易:) yay – Ortund