문제

i know how to execute execCommand on some selected text, but is there a way to execute on some other text in my contenteditable div ?

도움이 되었습니까?

해결책

In IE, yes. In other browsers, no.

All major desktop browsers implement some form of document.execCommand(), which acts only on the user selection. In IE, the TextRange object also has an execCommand method. For example, the following will turn the text contents of an element green when the user hovers the cursor over it:

<div id="test">Here is a test div</div>

<script type="text/javascript">
    var div = document.getElementById("test");
    div.onmouseover = function() {
        if (document.body.createTextRange) {
            var textRange = document.body.createTextRange();
            textRange.moveToElementText(div);
            textRange.execCommand("foreColor", false, "green");
        }
    };
</script>

다른 팁

In case you are not aware, the .execCommand is an IE only non-standard javascript function that should be avoided.

If you tell us what you want to use it for, then we could suggest a standards-compliant alternative.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top