为一个div设置execcommand

时间:2011-03-05 17:39:07

标签: javascript execcommand

它有任何方法绑定execcommand与div元素不是为整个文档,我试试这个:

document.getElementById('div').execcommand(...)

但它有一个错误:

execcommand is not a function

它有任何方法将execcommand与div元素绑定而不是整个文件!! 我不喜欢使用iframe方法。

4 个答案:

答案 0 :(得分:17)

这在IE中比其他浏览器更容易,因为IE的TextRange对象具有execCommand()方法,这意味着可以在文档的某个部分上执行命令,而无需更改选择和暂时启用designMode(这是您在其他浏览器中必须执行的操作)。这是一个干净利落的功能:

function execCommandOnElement(el, commandName, value) {
    if (typeof value == "undefined") {
        value = null;
    }

    if (typeof window.getSelection != "undefined") {
        // Non-IE case
        var sel = window.getSelection();

        // Save the current selection
        var savedRanges = [];
        for (var i = 0, len = sel.rangeCount; i < len; ++i) {
            savedRanges[i] = sel.getRangeAt(i).cloneRange();
        }

        // Temporarily enable designMode so that
        // document.execCommand() will work
        document.designMode = "on";

        // Select the element's content
        sel = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);

        // Execute the command
        document.execCommand(commandName, false, value);

        // Disable designMode
        document.designMode = "off";

        // Restore the previous selection
        sel = window.getSelection();
        sel.removeAllRanges();
        for (var i = 0, len = savedRanges.length; i < len; ++i) {
            sel.addRange(savedRanges[i]);
        }
    } else if (typeof document.body.createTextRange != "undefined") {
        // IE case
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.execCommand(commandName, false, value);
    }
}

示例:

var testDiv = document.getElementById("test");
execCommandOnElement(testDiv, "Bold");
execCommandOnElement(testDiv, "ForeColor", "red");

答案 1 :(得分:4)

也许这可以帮助你:

此页面上的

Example code 1通过使用函数在div上具有execcommand。不确定这是不是你要追求的?祝你好运!

编辑:我想出了如何把代码放在这里:o

<head>
<script type="text/javascript">
    function SetToBold () {
        document.execCommand ('bold', false, null);
    }
</script>
</head>
<body>
      <div contenteditable="true" onmouseup="SetToBold ();">
       Select a part of this text!      
      </div>
</body>

答案 2 :(得分:3)

使用Javascript:

var editableFocus = null;

  window.onload = function() {
    var editableElements = document.querySelectorAll("[contenteditable=true]");

    for (var i = 0; i<editableElements.length; i++) {
      editableElements[i].onfocus = function() {
        editableFocus = this.id;
      }
    };
  }

  function setPreviewFocus(obj){
    editableFocus = obj.id
  }

  function formatDoc(sCmd, sValue) {
    if(editableFocus == "textBox"){
      document.execCommand(sCmd, false, sValue); 
    }
  }

HTML:

<div id="textBox" contenteditable="true">
Texto fora do box
</div>

<div contenteditable="true">
Texto fora do box
</div>

<button title="Bold" onclick="formatDoc('bold');">Bold</button>

答案 3 :(得分:1)

您可以保持简单,并将其添加到您的div。

<div contenteditable="true" onmouseup="document.execCommand('bold',false,null);">