2011-05-10 55 views
5

我需要使用javascript將文本框的選定文本加粗/斜體/下劃線。爲此,我使用下面的代碼。需要使用javascript將選定的文本設置爲粗體/斜體/下劃線,並使用c#保存並檢索相同的文本。

<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> 

但這裏的問題是,當我點擊大膽圖像上的使得整個文本加粗而不是選定的文本。對另外兩張圖片也不適用。

同時節約文本框的文字我即使

document.getElementById('TextBox1').innerHTML; 

我能夠得到文本框的唯一值後嘗試我無法獲得包括HTML標籤的文本。

有什麼辦法來保存和使用JavaScript或C#

提前感謝 檢索相同的SC

+0

我只是在這個線程迷迷糊糊地想讓你們知道,[這個職位上的SO(http://stackoverflow.com/questions/ 717224 /如何獲取選定的文本在textarea)可能會幫助你。 – luk2302 2013-07-15 14:25:23

回答

2

這裏是回答了關於讓高亮顯示的文字 How to get selected text in textarea?

您的問題一個問題關於使所選文本變粗體,您需要使用html標記或bbcode之類的東西,並在將它打印到頁面上時將其解析爲html。

編輯:Here is a page that shows the jquery plugin "fieldselection" in action.

編輯2:這裏是我會怎麼做了一個例子是:jsfiddle link

的HTML:

<input id="bold" type="button" value="B" /> 
<br /> 
<textarea id="editor"></textarea> 

<div id="resultAsHtml"></div> 
<br /> 
<div id="resultAsText"></div> 

的JavaScript(jQuery的)代碼:

$(document).ready(function() { 

    $("#editor").keyup(Update); 

    function Update(){ 
     var text = $(this).val(); 
     var result = ParseToHtml(text); 
     $("#resultAsHtml").html(result); 
     $("#resultAsText").text(result); 
    } 

    $("#bold").click(function(){ 
     var range = $("#editor").getSelection(); 
     var textToReplaceWith = "[b]"+ range.text + "[/b]"; 
     $("#editor").replaceSelection(textToReplaceWith , true); 

     var text = $("#editor").val(); 
     var result = ParseToHtml(text); 
     $("#resultAsHtml").html(result); 
     $("#resultAsText").text(result); 
    }); 

    function ParseToHtml(text) { 
     text = text.replace("[b]", "<b>"); 
     text = text.replace("[/b]", "</b>"); 
     text = text.replace(" ","&nbsp;"); 
     text = text.replace("\n","</br>"); 
     return text; 
    } 

    $("#bold").replaceSelection("[b]" + $("#editor").getSelection() + "[/b]", true); 
}); 
+0

嗨,Joakim,謝謝你的回覆。但是我無法獲得選定的文本。只要我點擊粗體圖像,選定的文本就會被取消選中。 你能幫我嗎。 – DotnetDude 2011-05-11 04:27:54

+0

查看我添加到我的答案的鏈接,其中顯示瞭如何在我的答案的第一個鏈接中使用引用的jquery插件。 – Joakim 2011-05-11 06:15:45

+0

嗨Joakim, 與他們提供的鏈接,我cpould只獲得選定的文本,但我不能格式化(使其粗體)並將bak放在同一個文本框中。如果它不可能與文本框,是否有任何其他控件,我可以在其中格式化文本。請指教。 – DotnetDude 2011-05-12 04:38:19

1

document.execCommand(「bold」,false,null); 這是在所有的瀏覽器工作對我來說最簡單的 ... techinique

相關問題