Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 所见即所得文本编辑器的制作_Javascript_Jquery_Iframe_Wysiwyg_Text Editor - Fatal编程技术网

Javascript 所见即所得文本编辑器的制作

Javascript 所见即所得文本编辑器的制作,javascript,jquery,iframe,wysiwyg,text-editor,Javascript,Jquery,Iframe,Wysiwyg,Text Editor,我想做我自己的文本编辑器。我正试图仿效其他几个成熟编辑器的例子,使用隐藏的和可见的组合 我在键入的第一步遇到了麻烦。我可以让execCommand处理可编辑的内容,例如,将文本设置为粗体,但在上执行时遇到问题 这是我的HTML: <html> <button type="button" class="btn btn-bold">bold</button> <textarea id="input" name="input" style="display:

我想做我自己的文本编辑器。我正试图仿效其他几个成熟编辑器的例子,使用隐藏的
和可见的
组合

我在键入
的第一步遇到了麻烦。我可以让execCommand处理可编辑的内容,例如,将文本设置为粗体,但在
上执行时遇到问题

这是我的HTML:

<html>
<button type="button" class="btn btn-bold">bold</button>
<textarea id="input" name="input" style="display: none;">Start typing..</textarea>
<iframe frameborder="0" src="javascript:true;" style="">  </iframe>
</html>
我知道我需要对iframe文档而不是父文档执行execCommand,但不确定如何执行。目前,此代码不允许我设置iframe的关键帧,因此无法测试粗体按钮的效果

任何想法都将不胜感激

使用
并将所选内容环绕为粗体。您也可以使用此方法执行其他任务

演示:

编辑 如何使iframe可编辑:

window.frames[0].document.getElementsByTagName("body")[0].contentEditable = true;​
  • -带粗体按钮

    • 这可能会有所帮助。但它是用javascript编写的,我正在谷歌chrome上运行。下面是代码:

      <div>
        <input type="button" onClick="bo()" value="B"> 
        <input type="button" onClick="under()" value="U">
        <input type='button' onClick="f_size()" value="f_size">
      
      <br>
      <iframe name="richTextField" id="richTextField" style="border:#000000 1px solid; width:700px; height:300px;"></iframe><br> 
      <input type="button" onClick="asd()" value="get_innerhtml"><br> 
      <textarea name="myTextArea" id="myTextArea" cols="100" rows="14"></textarea>
      </div>
      <script>
      richTextField.document.designMode = 'On';
      
      
      function bo(){
          richTextField.document.execCommand('bold',false,null); 
      }
      function under(){
          richTextField.document.execCommand('underline',false,null);
      }
      function f_size(){
          var size = prompt('Enter a size 1 - 7', '');
          richTextField.document.execCommand('FontSize',false,size);
      }
      function asd(){
      
      var text=document.getElementById('richTextField').contentWindow.document.body.innerHTML;
      document.getElementById('myTextArea').value=text;
      }
      </script>
      
      
      


      richTextField.document.designMode='On'; 函数bo(){ richTextField.document.execCommand('bold',false,null); } 在()下的函数{ richTextField.document.execCommand('underline',false,null); } 函数f_size(){ 变量大小=提示('输入大小1-7',''); richTextField.document.execCommand('FontSize',false,size); } 函数asd(){ var text=document.getElementById('richTextField').contentWindow.document.body.innerHTML; document.getElementById('myTextArea')。值=文本; }
      如果您正在创建一个真正的文本编辑器(而不是富文本编辑器),那么最好的解决方案是使用
      (在实际页面中,没有
      IFRAME
      )作为文本垫。有了这个元素,你也可以使用真正的标签。嗨,蒂姆,我想创建一个富文本编辑器,但是谢谢你的输入,你对如何键入
      iframe
      有什么建议吗?好吧,就像德里克在上一篇评论中说的,在
      iframe
      中加载一个页面,这样你就真的有东西可参考了。然后你必须学习很多关于选择、TextRange对象和jQuery提供的选择事件等。嗨,德里克,我宁愿学习第一原则,但谢谢你的建议。你对如何设置iframe的键有什么建议吗?@tim peterson,包装元素非常简单和基本。但是,如果您想知道如何在帧中设置关键帧,下面是如何设置的:在
      iframe
      中设置一个事件侦听器,然后在那里执行您想要的任何操作。(我真的不明白你所说的
      keyup
      是什么意思)我的意思是我希望iframe是
      内容可编辑的
      ,这样当我点击它时,我就可以实际键入。你可以在
      中创建一个
      内容可编辑的
      。嗨,德里克,谢谢你的演示,太棒了!我刚刚意识到iOS Safari不支持
      contenteditable
      ,因此我需要一种变通方法,允许人们在移动设备上仍然输入文本并对其进行格式化(加粗等)。我测试了所见即所得富文本编辑器Cleditor:使用我的iPhone,我可以在他们的演示编辑器框中输入文本。我想,为了模仿这一点,我只需要根据用户代理切换
      textarea
      iframe
      的可见性?
      <div>
        <input type="button" onClick="bo()" value="B"> 
        <input type="button" onClick="under()" value="U">
        <input type='button' onClick="f_size()" value="f_size">
      
      <br>
      <iframe name="richTextField" id="richTextField" style="border:#000000 1px solid; width:700px; height:300px;"></iframe><br> 
      <input type="button" onClick="asd()" value="get_innerhtml"><br> 
      <textarea name="myTextArea" id="myTextArea" cols="100" rows="14"></textarea>
      </div>
      <script>
      richTextField.document.designMode = 'On';
      
      
      function bo(){
          richTextField.document.execCommand('bold',false,null); 
      }
      function under(){
          richTextField.document.execCommand('underline',false,null);
      }
      function f_size(){
          var size = prompt('Enter a size 1 - 7', '');
          richTextField.document.execCommand('FontSize',false,size);
      }
      function asd(){
      
      var text=document.getElementById('richTextField').contentWindow.document.body.innerHTML;
      document.getElementById('myTextArea').value=text;
      }
      </script>