js几个常用技巧

(一).确认删除的几种常用方法:
1.给按钮的onclick属性加上确认框,比如一个删除按钮的id是BtnDel,则代码如下:

  
  
BtnDel.Attributes.Add( " onclick " , " return confirm(' " + " 确认删除? " + " ') " );

 

  
  
BtnDel.Attributes[ " onclick " ] = " javascript:return confirm(' " + " 确认删除? " + " '); " ;

 

2.方法如下:

代码
   
   
< script language = " JavaScript " type = " text/JavaScript " >

function delete_y(e)

{

if (event.srcElement.outerText == " 删除 " )

event.returnValue
= confirm( " 确定删除? " );

}

document.onclick
= delete_y;

< / script>

 

2.屏蔽功能类
屏蔽键盘所有键

  
  
< script language = " javascript " >

function document.onkeydown(){

event.keyCode
= 0 ;

event.returnvalue
= false ;

}

< / script>

屏蔽鼠标右键
在body标签里加上oncontextmenu=self.event.returnvalue=false 或者

  
  
< script language = " javascript " >

function document.oncontextmenu()

{

return false ;

}

< / script>

 

  
  
function nocontextmenu()

{

if (document.all) {

event.cancelBubble
= true ;

event.returnvalue
= false ;

return false ;

}

}

 

代码
   
   
< script language = " javascript " >

function rclick()

{

if (document.all) {

if (event.button == 2 ){

event.returnvalue
= false ;

}

}

}

< / script>

 

屏蔽 Ctrl+N、Shift+F10、F5刷新、退格键

代码
   
   
< script language = " javascript " >

// 屏蔽鼠标右键、Ctrl+N、Shift+F10、F5刷新、退格键

function window.onhelp(){ return false } // 屏蔽F1帮助

function KeyDown(){

if ((window.event.altKey) &&

((window.event.keyCode
== 37 ) || // 屏蔽 Alt+ 方向键 ←

(window.event.keyCode
== 39 ))){ // 屏蔽 Alt+ 方向键 →

alert(
" 不准你使用ALT+方向键前进或后退网页! " );

event.returnvalue
= false ;

}



/* 注:这还不是真正地屏蔽 Alt+ 方向键,

因为 Alt+ 方向键弹出警告框时,按住 Alt 键不放,

用鼠标点掉警告框,这种屏蔽方法就失效了。以后若

有哪位高手有真正屏蔽 Alt 键的方法,请告知。
*/



if ((event.keyCode == 8 ) &&

(event.srcElement.type
!= " text " &&

event.srcElement.type
!= " textarea " &&

event.srcElement.type
!= " password " ) || // 屏蔽退格删除键

(event.keyCode
== 116 ) || // 屏蔽 F5 刷新键

(event.ctrlKey
&& event.keyCode == 82 )){ // Ctrl + R

event.keyCode
= 0 ;

event.returnvalue
= false ;

}

if ((event.ctrlKey) && (event.keyCode == 78 )) // 屏蔽 Ctrl+n

event.returnvalue
= false ;

if ((event.shiftKey) && (event.keyCode == 121 )) // 屏蔽 shift+F10

event.returnvalue
= false ;

if (window.event.srcElement.tagName == " A " && window.event.shiftKey)

window.event.returnvalue
= false ; // 屏蔽 shift 加鼠标左键新开一网页

if ((window.event.altKey) && (window.event.keyCode == 115 )){ // 屏蔽Alt+F4

window.showModelessDialog(
" about:blank " , "" , " dialogWidth:1px;dialogheight:1px " );

return false ;}

}

< / script>

 

你可能感兴趣的:(js)