简体   繁体   English

如何在html页面中使用Ctrl + A选择所有表列?

[英]How to select all table columns using Ctrl+A in a html page?

我们可以通过使用jQuery或javascript按下CTRL + A按钮来选择所有表td吗?

Yes you can, and you are gonna have to do it programmatically. 是的,您可以,并且您将必须以编程方式进行操作。

Here is an example: Handling Keybord Shortcuts in Javascript 这是一个示例: 用Javascript处理键盘快捷键

And yes, this is a good question. 是的,这一个好问题。

You can use this function to catch Ctrl + A or Ctrl + a . 您可以使用此功能捕获Ctrl + ACtrl + a

$("body").keydown(function(e) {
    if (e.ctrlKey && (e.keyCode == 65 || e.keyCode == 97)) {
        selectText('copyme');
        e.preventDefault();
    }
});

This function will work in Firefox and Chrome. 此功能将在Firefox和Chrome中运行。 The old version used keypress() function and e.charCode and didn't work in newer version of Chrome. 旧版本使用e.charCode keypress()函数和e.charCode ,但在新版本的Chrome中不起作用。

The only issue is with selecting text. 唯一的问题是选择文本。 I've borrowed the function selectText() from this answer https://stackoverflow.com/a/987376/446792 我已经从这个答案中借用了函数selectText() https://stackoverflow.com/a/987376/446792

Here is a working demo: 这是一个工作示例:

 $(document).ready(function() { function selectText(element) { var doc = document; var text = doc.getElementById(element); if (doc.body.createTextRange) { // ms var range = doc.body.createTextRange(); range.moveToElementText(text); range.select(); } else if (window.getSelection) { // moz, opera, webkit var selection = window.getSelection(); var range = doc.createRange(); range.selectNodeContents(text); selection.removeAllRanges(); selection.addRange(range); } } $("body").keydown(function(e) { if (e.ctrlKey && (e.keyCode == 65 || e.keyCode == 97)) { selectText('copyme'); e.preventDefault(); } }); }); 
 table, td { border: solid 1px black; margin: 10px; padding: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent iaculis lobortis adipiscing. Donec consequat commodo posuere. Praesent magna orci, suscipit ut facilisis sed, volutpat in lorem. Nulla eleifend.</p> <table id="copyme"> <tr><td>A</td><td>B</td><td>C</td></tr> <tr><td>D</td><td>E</td><td>F</td></tr> </table> <p>Vestibulum ac commodo libero. Aenean vitae magna nulla. Vivamus hendrerit, orci sed pretium aliquam.</p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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