應用程序:我的頁面中有一個textarea元素。它使用CodeMirror進行轉換,因爲我需要縮進並突出顯示html代碼。 PIC是在鏈接: [textarea的使用codemirror]當我使用CodeMirror在TextArea中編輯代碼時如何使用js或jQuery將此代碼反映到另一個textarea中
http://uploadingit.com/file/gxftd2cps9wm7zhp/cm.png
這裏是textarea的代碼使用codemirror:
</textarea>
<button type="submit">Post</button>
</form>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: { name: "xml", htmlMode: true },
lineNumbers: true,
tabMode: "indent",
matchBrackets: true,
path: 'js/',
searchMode: 'inline',
onCursorActivity: function() {
editor.setLineClass(hlLine, null);
hlLine = editor.setLineClass(editor.getCursor().line, "activeline");
}
});
var hlLine = editor.setLineClass(0, "activeline");
</script>
怎麼辦呢?:當我點擊發布按鈕,所有的代碼應該被傳輸到另一個textarea。我需要這樣做與JavaScript或jQuery,但掙扎如何做到這一點。任何幫助?
對於真實世界的應用程序此textarea中的代碼(來自上圖)應該會影響Html Designer API。即Html設計器中的任何更改都應反映在此textarea中(爲了便於閱讀,它使用了codemirror),反之亦然。當我在textarea中編輯更改時應該體現在HtmlDesigner中,但是在這個模擬案例中 - >在第二個textarea中。
例如:像Visual Studio .Net網頁代碼編輯器,它具有Designer + Source模式。現在很清楚了嗎?
我在問如何使用javascript或jquery實現上述機制。非常感謝!
謝謝。這是行得通的,但行爲: –
代碼出現在第二個textarea從第一次只有當我把代碼放入Visual Studio中的textarea。但在現實世界中,該網站的用戶將在第一個textarea中插入一些代碼,並在第二個txtarea中出現以編輯它,然後按下Post按鈕。它應該去第一個。那麼如何檢測用戶插入的內容以及如何用JS控制呢? :) 謝謝。 –
嗯,這很有趣......你點擊按鈕時,textarea仍然被選中?如果是這樣,請點擊按鈕之前嘗試點擊文本區域(因此模糊)。如果它仍然無法正常工作,那麼我會認爲它是一個CodeMirror特性,它返回的是舊值而不是新值 - 按照Marijn的建議嘗試getValue()可能是個好主意。 –