2016-06-21 23 views
1

由於與MathJax相關的原因,我需要動態清除Contenttools可編輯區域中的內容,然後將新元素附加到可編輯區域。當我將元素添加到該區域時,它們不會被Contenttools編輯。我在AJAX回調中使用以下jQuery代碼:Contenttools - 通過jQuery附加數據

$(".contentArea").empty().append($(response.data.content)) 

內容顯示爲我想要但不可編輯。誰能幫我?我也嘗試在新內容加載後使用editor.init(...)重新啓動編輯器,但似乎沒有註冊新內容。任何幫助是極大的讚賞!

+0

幾個快速問題:當您嘗試替換區域的內容時,CT編輯器是否處於活動狀態(例如,您在編輯)?您插入的內容是否在插入之前或您嘗試重新初始化編輯器之前由MathJax格式化? –

+0

@AnthonyBlackshaw當MathJax被渲染時,它將TeX轉換爲MathML。我不想編輯MathML標記,而是編輯TeX(例如元素的innerHTML中的$ x^2 + 7 $)。 我試圖執行以下操作:當編輯器啓動時,將頁面內容重新加載到頁面中(以去除MathML標記,將其返回到TeX標記),然後開始編輯該內容。因此,我試圖在編輯器啓動時替換內容(我將其綁定到editor._ignition的'start'事件)。我很感激幫助。 –

+0

嗨@Grant桑德斯 - 我看到你是新來的SO。如果您覺得答案可以解決問題,請點擊綠色複選標記將其標記爲「已接受」。這有助於將注意力集中在仍然沒有答案的舊版SO上。 –

回答

1

確定這聽起來像是發生了什麼事是,當你請求的內容你特克斯版本替換當前MATHML編輯器中啓動,所以當你更換可編輯區域中的內容是活導致編輯器和頁面不同步。

我不能很容易地爲你的確切情況嘗試這一點,但下面的代碼概述了我將採取的最初方法來解決你所描述的,如果你讓我知道你如何得到,如果需要修改我會很高興地更新它。

var editor = ContentTools.EditorApp.get(); 

// Add a flag to the editor that indicates when the tex version of the content 
// has loaded. 
editor.texLoaded = false; 

// Capture the start event against the editor, the first time around we load 
// the tex version of our content, the second time around we intialize the 
// editor as normal. 
editor.addEventListener('start', function(ev) { 

    // Has the Tex content been loaded, if so do nothing we're ready to start 
    // editing. 
    if (this.texLoaded) { 
     return; 
    } 

    // If the Tex content hasn't been loaded then cancel the start event 
    ev.preventDefault(); 

    // Load the Tex content 
    $.ajax({ 
     url: "/get-tex?..." 
    }).done(function (response) { 
     // From the response update the contents of the editable region 
     $('.contentArea')[0].innerHTML = response.data.content; 

     // Flag that the tex version of the content is now in place 
     this.texLoaded = true; 

     // Start the editor 
     editor.start(); 
    }); 
}); 
+0

這個工程!我只需在「停止」和「保存」事件偵聽器中切換this.texLoaded的值,以便後續編輯具有相同的功能。謝謝! –

0

內容正在加載,但你不能編輯? 嘗試這種方式

$(".contentArea").empty().append($(response.data.content)); 
//Reset the editable function