源码-JavaScript&jQuery交互式前端开发-第6章-事件-Focus和blur事件

Focus和blur事件即控件获取及失去焦点时触发的事件。


JS如下:

function checkUsername() {                        // Declare function
  var username = el.value;                        // Store username in variable
  if (username.length < 5) {                      // If username < 5 characters
    elMsg.className = 'warning';                  // Change class on message
    elMsg.textContent = 'Not long enough, yet...';// Update message
  } else {                                        // Otherwise
    elMsg.textContent = '';                       // Clear the message
  }
}

function tipUsername() {                          // Declare function
  elMsg.className = 'tip';                        // Change class for message
  elMsg.innerHTML = 'Username must be at least 5 characters'; // Add message
}

var el = document.getElementById('username');     // Username input
var elMsg = document.getElementById('feedback');  // Element to hold message

// When the username input gains / loses focus call functions above:
el.addEventListener('focus', tipUsername, false); // focus call tipUsername()
el.addEventListener('blur', checkUsername, false);// blur call checkUsername()

/* LONGER VERSION WITH IE8 (and lower) compatibility

if (el.addEventListener) {
  el.addEventListener('focus', tipUsername, false);
  el.addEventListener('blur', checkUsername, false);
} else {
  el.attachEvent('onfocus', tipUsername);
  el.attachEvent('onblur', checkUsername);
}

*/


HTML如下:

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript & jQuery - Chapter 6: Events -  Focus and Blur</title>
    <link rel="stylesheet" href="css/c06.css" />
  </head>
  <body>
    <div id="page">
      <h1>List King</h1>
      <h2>New Account</h2>
      <form method="post" action="http://www.example.org/register">

        <label for="username">Create a username: </label>
        <input type="text" id="username" /><div id="feedback"></div>

        <label for="password">Create a password: </label>
        <input type="password" id="password" />

        <input type="submit" value="sign up" />

      </form>
    </div>
    <script src="js/focus-blur.js"></script>
  </body>
</html>


CSS如下:

@import url(http://fonts.googleapis.com/css?family=Oswald);

body {
  background-color: #000;
  font-family: 'Oswald', 'Futura', sans-serif;
  margin: 0; 
  padding: 0;}

#page {
  background-color: #403c3b;
  margin: 0 auto 0 auto;}
  /* Responsive page rules at bottom of style sheet */

h1 {
  background-image: url(../images/kinglogo.png);
  background-repeat: no-repeat;
  background-position: center center;
  text-align: center;
  text-indent: -1000%;
  height: 75px;
  line-height: 75px;
  width: 117px;
  margin: 0 auto 0 auto;
  padding: 30px 10px 20px 10px;}

h2 {
  color: #fff;
  font-size: 24px;
  font-weight: normal;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: .3em;
  margin: 0 0 23px 0;}

h2 span {
  background-color: #fff;
  color: #000;
  font-size: 12px;
  text-align: center;
  letter-spacing: 0;
  display: inline-block;
  position: relative;
  border-radius: 50%;
  top: -5px;
  height: 22px;
  width: 26px;
  padding: 4px 0 0 0;}

ul {
  background-color: #584f4d;
  border:none;
  padding: 0;
  margin: 0;}

li {
  background-color: #ec8b68;
  color: #fff;
  border-top: 1px solid #fe9772;
  border-bottom: 1px solid #9f593f;
  font-size: 24px;
  letter-spacing: .05em;
  list-style-type: none;
  text-shadow: 2px 2px 1px #9f593f;
  height: 50px;
  padding-left: 1em;
  padding-top: 10px;}

li a {
  color: #fff;
  background-image: url('../images/icon-link.png');
  background-position: right, center;
  background-repeat: no-repeat;
  text-decoration: none;
  padding-right: 36px;}

li.complete a {
  background-image: none;}

p {
  background-color: #7f675c;
  color: #e3dfdd; 
  padding: 10px;
  margin: 20px auto;
  min-width: 20%; 
  max-width: 80%; 
  border-radius: 5px;
  text-align: center;
  text-shadow: -1px 1px 0 #333;
  -webkit-box-shadow: inset 1px -1px 0 0 #a5948c;
  box-shadow: inset 1px -1px 0 0 #a5948c;}

.hot {
  background-color: #d7666b;
  color: #fff;
  text-shadow: 2px 2px 1px #914141;
  border-top: 1px solid #e99295;
  border-bottom: 1px solid #914141;}

.cool {
  background-color: #6cc0ac;
  color: #fff;
  text-shadow: 2px 2px 1px #3b6a5e;
  border-top: 1px solid #7ee0c9;
  border-bottom: 1px solid #3b6a5e;}

.complete {
  background-color: #999;
  color: #fff;
  background-image: url("../images/icon-trash.png");
  background-position: right, center;
  background-repeat: no-repeat;
  border-top: 1px solid #666;
  border-bottom: 1px solid #b0b0b0;
  text-shadow: 2px 2px 1px #333;}

.complete a {display:block;}


/* Form styles */

form {
  padding: 0 60px 65px 60px;}

label {
  color: #fff;
  display: block;
  margin: 10px 0 10px 0;
  font-size: 24px;}

input[type='text'], input[type='password'], textarea {
  background-color: #999;
  color: #666;
  font-family: 'Oswald', 'Futura', sans-serif;
  font-size: 24px;
  width: 96%;
  padding: 4px 6px;
  border: 1px solid #999;
  border-radius: 8px;}

input[type='text']:focus, input[type='password']:focus, textarea:focus {
  border: 1px solid #fff;
  background-color: #fff;
  outline: none;}

input[type='submit'], a.add {
  background-color: #cb6868;
  color: #f3dad1;
  border: none;
  border-radius: 5px;
  padding: 8px 10px;
  margin-top: 10px;
  float: right;
  font-size: 18px;
  text-decoration: none;
  text-transform: uppercase;}

input[type='submit']:hover, a.add:hover {
  background-color: #d75359;
  color: #f3dad1;
  cursor: pointer;
  box-shadow: none;
  position: relative;
  top: 1px;}

textarea {
  width: 96%;
  height: 5em;
  line-height: 1.4em;}

select, option {
  font-size: 16px;}


#feedback, #termsHint {
  color: #fff;
  background-image: url('../images/warning.png');
  background-repeat: no-repeat;
  background-position: 2px 14px;
  padding: 10px 0 0 22px;}

#feedback.warning {background-image: url('../images/warning.png');}
#feedback.tip {background-image: url('../images/tip.png');}

#packageHint {
  color: #fff;
  background-image: url('../images/hint.png');
  background-repeat: no-repeat;
  background-position: 2px 5px;
  padding-left: 22px;}

.selectbox {
  display: inline;}

.checkbox {
  display: inline;
  font-size: 16px;}

/* Click event example - note style */
#note {
  color: #fff;
  background-color: #000;
  opacity: 0.9;
  box-shadow: -3px 3px 6px #000;
  padding: 1em 1em 2em 1em;
  height: 95%;
  width: 95%;
  position: absolute;
  top: 1em;
  left: 0;}

#note .header + div {
  background: rgb(255, 255, 255);
  background: rgba(255, 255, 255, 1);
  position: relative;
  text-align: center;
  top: 1px;
  color: #d7666b;
  padding: 20px;
  margin: 30px;
  border: 1px solid #d7666b;}

#note h2 {
  color: #d7666b;
  padding-top: 20px;
  padding-bottom: 20px;}

#note a {
  text-decoration: none;
  color: #fff;}

#note .header {
  text-align: right;
  background-color: #000;
  border: none;
  padding: 0 0 0 10px;}

.shown {
  visibility: visible;}

.hidden {
  visibility: hidden;}


/* Position Example */
#stats {
  background-color: #6cc0ac;
  color: #ffffff; 
  position: fixed; 
  top: 0; 
  left: 0; 
  padding: 20px; 
  width: 100%;}

#stats input[type="text"] {
  width: 80px;
  margin: 0 10px 0 10px;
  background-color: #fff;
  border-color: #fff;}

#body #page {margin-top: 130px;}

.divider {
  margin: 0 40px; 
  border-left: 1px solid #fff; 
  border-right: 1px solid #fff; 
  color: #6cc0ac;}


/* Character Counter */
#charactersLeft {
  color: #fff;
  font-size: 24px;}
#lastKey {
  color: #fff;
  margin-top: 10px;}


/* Membership Options */
#packageHint, #termsHint {
  color: #fff;
  text-shadow: -1px 1px 2px #000;}

#terms {
  margin-top: 30px;}

/* Mutation */
#list, #list2 {
  margin-bottom: 10px;}
div.button {
  height: 65px;
  padding-right: 10px;}

/* Example */
a[data-state='stop'] {background-image: url('../images/pause.png');}
a[data-state='record'] {background-image: url('../images/record.png');}
#buttons {
  height: 100px;
  width: 100px;
  margin: 0 auto;}
#buttons a {
  width: 100px;
  height: 100px; 
  text-indent: 100%;
  margin-top: 20px;
  display: inline-block;
  white-space: nowrap;
  overflow: hidden;}


/* Small screen - acts like the app would */
@media only screen and (max-width: 500px) {
  body {
    background-color: #403c3b;
  }
  #page {
    max-width: 480px;
  }
}
@media only screen and (min-width: 501px) and (max-width: 767px) {
  #page {
    max-width: 480px;
    margin: 20px auto 20px auto;
  }
}
@media only screen and (min-width: 768px) and (max-width: 959px) {
  #page {
    max-width: 480px;
    margin: 20px auto 20px auto;
  }
}
/* Larger screens act like a demo for the app */
@media only screen and (min-width: 960px) {
  #page {
    max-width: 480px;
    margin: 20px auto 20px auto;
  }
}
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) { 
  h1{
    background-image: url(../images/2xkinglogo.png);
    background-size: 72px 72px;
  }
}


不加载CSS时的显示效果:

加载CSS后的显示效果:



OK,完美了!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值