JS检查当图片不存在时显示默认图片和键盘大小写键状态

时间:2022-08-02 00:32:57

当图片不存在时显示默认图片

1 <script type="text/javascript"> 
2         var imgs = document.images; 
3         for(var i = 0;i < imgs.length;i++){ 
4             imgs[i].onerror = function(){ 
5                 this.src = "http://images2015.cnblogs.com/blog/66516/201511/66516-20151104233425055-1712623285.png"; 
6             } 
7         } 
8 </script>

或者:

1 <img src="" border="0" onerror="this.src=' http://images2015.cnblogs.com/blog/66516/201511/66516-20151104233425055-1712623285.png'"/>

 

键盘大小写键状态

 1 <script type="text/javascript">
 2             function detectCapsLock(event) {
 3                 var e = event || window.event;
 4                 var o = e.target || e.srcElement;
 5                 var oTip = o.nextSibling;
 6                 var keyCode = e.keyCode || e.which; // 按键的keyCode 
 7                 var isShift = e.shiftKey || (keyCode == 16) || false; // shift键是否按住
 8                 if (
 9                     ((keyCode >= 65 && keyCode <= 90) && !isShift) // Caps Lock 打开,且没有按住shift键 
10                     || ((keyCode >= 97 && keyCode <= 122) && isShift)// Caps Lock 打开,且按住shift键
11                 ) {
12                     oTip.style.display = '';
13                 }
14                 else {
15                     oTip.style.display = 'none';
16                 }
17             }
18  
19             window.onload = function () {
20                 document.getElementById('<%= txtUserName.ClientID %>').focus();
21                 document.getElementById('<%= txtPassword.ClientID %>').onkeypress = detectCapsLock;
22             };
23 </script>