js html文本框增加快捷键

如何使用JavaScript ,给文本框增加快捷键呢?

Ctrl+s

$textarea.keydown(function (event) {
//            console.log(event.keyCode)
            if ((event.keyCode == 83/*S*/||event.keyCode == 88/*X*/ )&& event.ctrlKey) {
                console.log('save');
                updateOneOption(this);
            }
        })

在聚焦文本框的情况下,按下Ctrl+X 或者Ctrl+S,就会触发js方法updateOneOption(this); 

 

给文本框增加回车事件

/***
 enter to to something specified by responseEvent
 */
var pressEnterTo = function pressEnterTo(event, responseEvent, arguments2) {
	if (!event || event == undefined) {
		event = window.event || arguments.callee.caller.arguments[0];
	}
    var eventTarget = com.whuang.hsj.getSrcElement(event);
	if (event.keyCode == 13 && typeof responseEvent === 'function') {//Enter
		if(arguments2==undefined){
			arguments2=eventTarget;
		}
		responseEvent(arguments2);
	}
};
/*

 调用实例:

 

 

回车+CTRL换行

/*
 * 回车+CTRL换行
 * 
 */
newline = function (event) {
    if (event.keyCode == 13 && event.ctrlKey) {
        if (document.selection) {
            var selectText = document.selection.createRange();
            if (selectText) {
                if (selectText.text.length > 0)
                    selectText.text += "\r\n";
                else
                    selectText.text = "\r\n";
                selectText.select();
            }
        }
        else {
            var obj = com.whuang.hsj.getSrcElement(event);
            obj.value += "\r\n";
        }
    }
};

 

 

你可能感兴趣的:(javascript)