2015-08-28 62 views
2

我使用jquery.colorfy https://github.com/cheunghy/jquery.colorfy來更改textarea中的關鍵詞顏色。設置jquery.colorfy後的Textarea的值

我可以設置值使用$('#textarea').text("value");來更改我的textarea值。

問題是:代碼$('#textarea').colorfy("markdown");後,命令:$('#textarea').text("value");已死亡。

有人能告訴我發生了什麼事嗎?非常感謝。

+0

嘗試互換他們,執行分配第一 – Sherlock

+2

這可能是插件隱藏原來'textarea'並顯示其他HTML元素。因此,無論您使用插件API來更改文本,還是查找哪個「span」,「div」或其他顯示的文本真的放在哪個位置。 – Regent

回答

0

嘗試這樣的:

$(document).ready(function(){ 
     $('#area').colorfy("markdown"); 
     $('#area').text("value");//will be hidden you can see by inspecting the html 
     $('.area').html("value");// will be visible to you 
    }); 

see the image attached 注:我從看到的例子是它設置顯示:無到textarea的,但creates a div with the same class which is the class of text area,並將其應用於對這些自己的CSS。

$(document).ready(function(){ 
 
     $('#area').colorfy("markdown"); 
 
     $("#click").click(function(){ 
 
\t   $('.area').html("value");// will be visible to you 
 
      }); 
 
    });
.area { 
 
     float: auto; 
 
     margin-left: auto; 
 
     margin-right: auto; 
 
     width: 200px; 
 
     max-width: 200px; 
 
     height: 200px; 
 
     border-color: black; 
 
     border-width: 2px; 
 
     border-style: solid; 
 
     padding: 8px; 
 
     font-size: 15px; 
 
     text-align: left; 
 
    }
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.min.js"></script> 
 
    <script type="text/javascript" src="http://cheunghy.github.io/jquery.colorfy/jquery.colorfy.js"></script> 
 
    <script type="text/javascript" src="http://cheunghy.github.io/jquery.colorfy/jquery.colorfy.markdown.js"></script> 
 
    <textarea id="area" class="area"> 
 
    hi 
 
    </textarea> 
 
<button id="click">click!</button>

+0

謝謝。但是我的目標是在用戶點擊按鈕後改變textarea的值。根據當前示例, – Jerry

+0

是您使用$('。area')。html(「any string」);你可以在任何喜歡的事件中使用它。 –

+0

哦,它工作正常!非常感謝:D 你能解釋一下「。」之間的區別嗎?和「#」?謝謝。 – Jerry

0

先做操作上的textarea文字應用colorfy之前或者張貼與span施加管理它與class

增加你寫$('#textarea').colorfy("markdown");後適用的顏色即。通過添加span and class修改textarea文本來定製css。測試過這個插件請看下面的截圖。

enter image description here

快速注:請dubug更根據自己的需要。

+0

謝謝。我明白了。但是在這種情況下我怎麼能改變textarea?我的目標是在按下按鈕時在textarea的末尾添加一個字符串。謝謝。 – Jerry