首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅为div设置execcommand

仅为div设置execcommand
EN

Stack Overflow用户
提问于 2011-03-06 01:39:07
回答 4查看 21.2K关注 0票数 9

它可以将execcommand绑定到div元素,而不是整个文档,我尝试这样做:

代码语言:javascript
复制
document.getElementById('div').execcommand(...)

但它有一个错误:

代码语言:javascript
复制
execcommand is not a function

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-06 20:07:55

在IE中执行此操作比在其他浏览器中更容易,因为IE的TextRange对象具有execCommand()方法,这意味着可以在文档的某一节上执行命令,而无需更改选择并临时启用designMode (这是您在其他浏览器中必须执行的操作)。这里有一个函数可以干净利落地做你想做的事情:

代码语言:javascript
复制
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);
    }
}

示例:

代码语言:javascript
复制
var testDiv = document.getElementById("test");
execCommandOnElement(testDiv, "Bold");
execCommandOnElement(testDiv, "ForeColor", "red");
票数 20
EN

Stack Overflow用户

发布于 2011-03-06 01:47:23

也许这对你有帮助:

此页面上的Example code 1通过使用函数在div上包含execcommand。不确定这是不是你要找的?祝好运!

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

代码语言:javascript
复制
<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>
票数 4
EN

Stack Overflow用户

发布于 2015-04-02 11:58:05

Javascript:

代码语言: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:

代码语言:javascript
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5205544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档