Ctrl键KeyUp事件,单击表格标题

时间:2017-07-05 09:13:41

标签: javascript jquery html-table

我想用<th>生成keyup事件。如果我点击<th>点击事件正在那里填充,但我的要求是生成事件,当我发布 CTRL 键时点击<th>区域我正在使用以下代码

<html>
<head>
<script
 src="https://code.jquery.com/jquery-3.2.1.min.js"
 integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
 crossorigin="anonymous"></script>
</head>
<body>
<table border="2">
<th>rollno</th>
<th>name</th>
<tr>
<td>4567</td>
<td>john</td>
 </tr>
</table>
 </body>
 <script>
 $('th').click(function(){
 alert("yes its working");
 });
 </script>
 <script>
 $('th').on('keyup', function(e) {
 if (e.keyCode ==17)
 {
 alert("OOps! ctr+click release not working");
 }  
 });
 </script>

 </html>

2 个答案:

答案 0 :(得分:0)

喜欢那个?

$('th').click(function(e){
    if(e.ctrlKey){
        alert("yes its working");
    }
});

编辑:

$( 'th' ).click( function ( e ) {

    if ( e.ctrlKey ) {

        $( window ).on( 'keyup' , function () {

            alert( "yes its working" );

            $( window ).off( 'keyup' );

        } );

    }
    else {
        alert( 'nop' );
    }

} );

答案 1 :(得分:0)

$(document).on('keyup',function(event){
 if(event.which=="17"){
    cntrlIsPressed = true;
}
else{cntrllsPressed=false;}
});
 $('th').on('click ', function(e) {
 if (cntrllsPressed==true)
 {
 alert("OOps! ctr+click release not working");
}
});
$(document).on('keydown',function(event){
    cntrlIsPressed =false;
});