2013-02-01 34 views
0
<html> 
    <head> 
     <script> 
      function insertHTMLCode() { 
       var htmlText = '<b>HI</b>'; 
       try { 
        document.execCommand('insertHTML', false, htmlText); 
       } catch(e) { 
        console.log(e); 
       } 
      } 
     </script> 
    </head> 
    <body> 
     <div> 
      <ys_content contenteditable="true"><!-- ~Client References --> 
       <font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><strong>More information coming soon...</strong> </font> 
      <!-- ~/Client References --> 
      </ys_content> 
     </div> 
     <input type="button" onclick="insertHTMLCode();" value="Click"> 
    </body> 
</html> 

上面的代碼總是拋出錯誤,當我試圖插入HTML內容到Firefox中的可編輯區域,但在鉻工作正常。組件返回故障代碼:0x80004005的(NS_ERROR_FAILURE)nsIDOMHTMLDocument.execCommand]

我的FF版本:18.0.1

+0

點擊可編輯區域,然後點擊按鈕。在Chrome中,它將html HI添加到鼠標光標位置,但在Firefox中它會引發錯誤。 – Roul

回答

0
<div contenteditable="true"> 
     <ys_content ><!-- ~Client References --> 
      <font size="2" face="Arial, Helvetica, sans-serif" color="#000000"><strong>More information coming soon...</strong> </font> 
     <!-- ~/Client References --> 
     </ys_content> 
    </div> 

您的包裝股利應設置爲CONTENTEDITABLE =「真」

+0

不,這裏的問題是:我像使用普通的HTML標籤一樣使用。某些瀏覽器不支持使用自定義標記進行編輯的內容。所以,它爲某些瀏覽器拋出錯誤。 – Roul