开发者

jQuery Method to select the contents of a whole page

How can I select the contents of the whole page using jQuery for later to be copied to clipboard and hence another WYSIWYG.

The case is:

$("#SelectAll").click(function(){
//CODE TO SELECT ALL THE CONTENTS OF THE CURRENT PAGE
/* PS:
$("body").focus();
$("body").select(); //doesn't work 
*/
});

Any help is appreciated.

Thanks

FOUND THE SOLUTION:

function selectAll()
  var e = document.getElementsByTagName('BODY')[0];
  var r = document.createRange();
  r.selectNodeContents(e);
  var s = window.getSelection(开发者_如何学运维);
  s.removeAllRanges();
  s.addRange(r);
}

This works in FF haven't tested in other browsers. Just need to call selectAll wherever I want.


if ('createRange' in document && 'getSelection' in window) {
    // firefox, opera, webkit
    var range= document.createRange();
    range.selectNodeContents(document.body);
    var selection= window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
} else if ('createTextRange' in document.body) {
    // ie
    document.body.createTextRange().select();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜