HI,
I test it with code below:
$(function () {
var isCtrl = false;
var bol = false;
$(document).keyup(function (e) {
if (e.keyCode == 17)
isCtrl = false;
if (bol) //ctrl 和 A 任何一个keyup触发
{
document.execCommand('bold', false, null);
alert(selectElement().toString());
document.focus();
bol = false; //设置为false
}
}).keydown(function (e) {
if (e.keyCode == 17)
isCtrl = true;
if (e.keyCode == 65 && isCtrl == true) {
bol = true; //设置为true,表明ctrl+a组合键已经被按下
}
});
});
function getSelection() {
return (!!document.getSelection) ? document.getSelection() :
(!!window.getSelection) ? window.getSelection() :
document.selection.createRange().text;
}
当ctrl+A键释放的时候,alert弹出selection的内容是整个页面的source code,包括page title and js and content. 所以我认为问题出在这里。你可以尝试用creatrange 方法限制ctrl+A所选内容,之后应该可以正常工作了。这里有个类似的讨论你可以参考一下:
http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse
希望对你有所帮助。
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a
href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.