简体   繁体   中英

Move cursor to the beginning of a designmode iframe

是否可以将插入符号移动到设计模式IFRAME的最开头(在第一个元素之前)?

Here's a function to do that. Pass in a reference to the <iframe> element.

Live demo: http://jsfiddle.net/aLP26/

Code:

function moveCursorToStart(iframeEl) {
    var win = iframeEl.contentWindow || iframeEl.contentDocument.defaultView;
    var doc = win.document;
    if (win.getSelection && doc.createRange) {
        var sel = win.getSelection();
        var range = doc.createRange();
        range.selectNodeContents(doc.body);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (doc.selection && doc.body.createTextRange) {
        var textRange = doc.body.createTextRange();
        textRange.collapse(true);
        textRange.select();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM