2016-11-08 100 views
0

我有一個iFrame來創建消息。但是,當我在輸入iFrame時按Enter鍵時,它會創建另一個div,但是,我希望它創建一個<br/>。我嘗試使用execCommand insertBrOnReturn,但我沒有得到它的工作。有任何想法嗎?提前致謝!iFrame中止輸入密鑰

中的jsfiddle在計算器並沒有真正的工作,但我有一個現有的「Craz1k0ek/d869w67b /」默認jsfiddle.net

function iFrameOn() { 
 
    richTextField.document.designMode = 'On'; 
 
    richTextField.document.body.style.fontFamily = 'Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif'; 
 
} 
 
function iBold() { 
 
    richTextField.document.execCommand('bold', false, null); 
 
} 
 
function iUnderline() { 
 
    richTextField.document.execCommand('underline', false, null); 
 
} 
 
function iItalic() { 
 
    richTextField.document.execCommand('italic', false, null); 
 
} 
 
function submit_form() { 
 
    var theForm = document.getElementById("myForm"); 
 
    theForm.elements["myTextArea"].value = window.frames['richTextField'].document.body.innerHTML; 
 
    theForm.submit(); 
 
}
<body onload="iFrameOn()"> 
 
    <form action="" name="myForm" id="myForm" method="post"> 
 
    <p> 
 
     Title 
 
     <input name="title" id="title" type="text" size="80" maxlength="80"> 
 
    </p> 
 
    <div id="wysiwyg_cp" style="padding:8px; width:700px;"> 
 
     <input type="button" onClick="iBold()" value="B"> 
 
     <input type="button" onClick="iUnderline()" value="U"> 
 
     <input type="button" onClick="iItalic()" value="I"> 
 
    </div> 
 
    <textarea style="display:none;" name="myTextArea" id="myTextArea" cols="100" rows="14"></textarea> 
 
    <iframe name="richTextField" id="richTextField" style="border:#000 1px solid; width:700px; height:300px;"></iframe> 
 
    <input name="myBtn" type="button" value="Submit Data" onClick="javascript:submit_form();"> 
 
    </form> 
 
</body>

回答

0

後,我改變了submit_form功能如下:

function submit_form() { 
    remove_divs(); 
    var theForm = document.getElementById("myForm"); 
    theForm.elements["myTextArea"].value = window.frames['richTextField'].document.body.innerHTML; 
    theForm.submit(); 
} 

然後我加入remove_divs函數如下:

function remove_divs() { 
    var frameHTML = window.frames['richTextField'].document.body.innerHTML; 
    frameHTML = frameHTML 
     .replace(/<div><br><\/div>/g, '') 
     .replace(/<div>/g, '<p>') 
     .replace(/<\/div>/g, '</p>'); 
    $(window.frames['richTextField'].document.body).html(frameHTML); 
} 

它將所有<div><br></div>元素全部替換爲空白,然後用<p>和所有</div>元素替換所有<div>元素與</p>。這將完全按照我的需要格式化字段中的文本。

然後,我有一個問題與設置DesignMode = 'On';我解決了放置額外的代碼在JavaScript文件,如下所示:

window.onload = function() { 
    window.frames['richTextField'].document.designMode = "On"; 
} 

我也得到了我如何獲得訪問richTextField一些輕微的警告,因此,我將所有richTextField.document.execCommand元素替換爲window.frames['richTextField'].document.execCommand

我希望這有助於!