最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - .net c# asp.net make part of Textbox text bold - Stack Overflow

programmeradmin6浏览0评论

I have a .NET C#/Aspx web application in which users fill in fields and the resulting form is emailed out. I have a requirement to allow users to highlight text and then make that text either bold or coloured or both.

I have done a bit of searching on the net and found that javascript might be the best option. However, when the script inserts the html tags - it doesn't render within the textbox.I know this is a limitation of the asp textbox but is there anyway around this?

<script type="text/javascript"> 

function formatText(tag) {
    var selectedText = document.selection.createRange().text;

    if (selectedText != "") {
        var newText = "<" + tag + ">" + selectedText + "</" + tag + ">";
        document.selection.createRange().text = newText;
    }
} 

I have a .NET C#/Aspx web application in which users fill in fields and the resulting form is emailed out. I have a requirement to allow users to highlight text and then make that text either bold or coloured or both.

I have done a bit of searching on the net and found that javascript might be the best option. However, when the script inserts the html tags - it doesn't render within the textbox.I know this is a limitation of the asp textbox but is there anyway around this?

<script type="text/javascript"> 

function formatText(tag) {
    var selectedText = document.selection.createRange().text;

    if (selectedText != "") {
        var newText = "<" + tag + ">" + selectedText + "</" + tag + ">";
        document.selection.createRange().text = newText;
    }
} 

Share Improve this question asked Oct 4, 2012 at 19:01 user1721064user1721064 212 bronze badges 2
  • Do you need the text to be bold immediately? Or are you okay with adding literal text like <b> to the textbox, and allowing it to be passed to the email? – Ian Commented Oct 4, 2012 at 19:06
  • I need it bold immeadiately (within the asp textbox) - I already have it inserting the bold tags before and after the highlighted text – user1721064 Commented Oct 5, 2012 at 8:17
Add a ment  | 

3 Answers 3

Reset to default 7

You'll need a rich text editor like ckeditor.

Instead reinventing the wheel, take a look at a rich text editor, like for example TinyMCE you can strip it down, so the user only have the options like bold or italic.

More info how to create a simple example can be found here:

Create a very simple TinyMCE wysiwyg Editor

Cannot be done in a Textbox.Go for any RichTextEditor. (CKEDITOR or CLEEDITOR)

发布评论

评论列表(0)

  1. 暂无评论