2010-04-30 46 views
2

因此,在我詢問之前,我閱讀了一些相關的問題,但無法找到我的問題的答案。希望這裏的一些JavaScript大師能夠找到這個並給我帶來光明。從poput表單插入文本到div中,並用javascript重寫插入的文本

我爲nicEdit創建了另一個按鈕,一個視頻按鈕,並希望在編輯器DIV中插入一些格式化的文本(注意:nicEdit具有內聯DIV,無iframe,無textarea)。

這是我的按鈕,從圖像按鈕重建:

var nicVideoOptions = { 

    buttons : { 

     'video' : {name : __('Insert Video'), type : 'nicEditorVideoButton'} //, tags : ['VIDEO:'] 

    }, 
    iconFiles : {'video' : '../movie.png'} 

}; 

var nicEditorVideoButton = nicEditorAdvancedButton.extend({ 
    addPane : function() { 
     this.vi = this.ne.selectedInstance.selElm().parentTag('A'); 
     this.addForm({ 
      '' : {type : 'title', txt : 'Insert Video URL'}, 
      'href' : {type : 'text', txt : 'URL', 'value' : 'http://', style : {width: '150px'}} 
     },this.vi); 
    }, 
    submit : function(e) { 
     var vidVal = this.inputs['href'].value; 
     if(vidVal == "" || vidVal == "http://") { 
      alert("Enter the video url"); 
      return false; 
     } 
     this.removePane(); 

     if(!this.vi) { 
      var tmp = 'javascript:nicVidTemp();'; 
      this.ne.nicCommand("insertVideo",tmp); 

      // still nothing works 
      //this.vi = this.findElm('VIDEO:','href',tmp); 
      //this.vi = this.setContent('[video:' + this.inputs['href'].value + ']'); 
      //nicEditors.findEditor('edit-comment').setContent('<strong>Some HTML</strong> here'); 
      //this.vi = this.setContent('<strong>Some HTML</strong> here'); 
      insertAtCaret(this.ne.selectedInstance, vidVal); 
     } 
     if(this.vi) { 
        // still nothing works 
      //this.vi.setAttributes({ 
       //vidVal : this.inputs['href'].value 
      //}); 
      //this.vi = this.setContent('[video:' + this.inputs['href'].value + ']'); 
      //this.vi = this.setContent('<strong>Some HTML</strong> here'); 
     } 
    } 


}); 

nicEditors.registerPlugin(nicPlugin,nicVideoOptions); 

按鈕是存在的,形式poput像圖像按鈕,這樣也沒關係。但不能將文本插入到DIV中。最終輸出將從此採取:('[video:' + this.inputs['href'].value + ']')並在編輯器DIV顯示爲:[video:http//some.com/video-url]

正如你看到的,我盲目地接觸一切:)

這插入摘自: http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/

function insertAtCaret(areaId,text) { var txtarea = document.getElementById(areaId); var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false)); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") strPos = txtarea.selectionStart; var front = (txtarea.value).substring(0,strPos); var back = (txtarea.value).substring(strPos,txtarea.value.length); txtarea.value=front+text+back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); range.moveStart ('character', strPos); range.moveEnd ('character', 0); range.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; } 

流程:我點擊按鈕,彈出一個窗體,填入輸入文本框,點擊查詢按鈕後文本應該出現在編輯器DIV中。

我希望我能說清楚。任何幫助將是非常appreaciated

感謝

+0

你開到使用JavaScript框架,像jQuery? – Benson 2010-04-30 04:14:24

+0

是的,如果可能的話。已經嘗試過自己沒有運氣。 nicEdit沒有向按鈕打印任何課程或ID時遇到困難。不得不遍歷,但jquery仍然無法與形式:( – swan 2010-04-30 05:47:49

+0

)有沒有控制頁面?如果你確實可以添加你自己的nicEdit ID。 – Benson 2010-04-30 06:05:01

回答

1

其實,我覺得我看到你的問題。 insertAtCaret函數正在等待一個ID,它將用於執行getElementById(id)。看起來你正在傳遞一個對nicEdit對象的引用。

我建議你要麼

  1. 創建自己的網頁上textarea的一個ID,並通過您的帳號。
  2. 修改insertAtCaret功能不是由ID搜索,而是使用在通過textarea的裁判。
+0

對於nicEditorAdvancedButton,我認爲我可以放入ID,就像'insert'一樣,但是如何使它與提交圖像按鈕ID不同?如果我在這裏做了所有提交按鈕, ('input')。setAttributes({'type':'submit','id':'insert'})。setStyle({backgroundColor:'#efefef',border:'1px solid# ccc',margin:'3px 0','float':'left','clear':'both'})。appendTo(this.form); – swan 2010-04-30 06:27:04

+0

嘗試上面介紹的選項2,基本上只需要移除該函數的第一行並將參數「areaId」的名稱更改爲「txtarea」。 – Benson 2010-04-30 20:41:35

+0

Thanks,bu我通過刪除函數並使用已有的函數來替代:this.ne.selectedInstance.setContent。不過,我仍然需要糾正驗證。 – swan 2010-05-02 13:30:30