溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用javascript模仿一個(gè)快遞單號(hào)查詢功能

發(fā)布時(shí)間:2020-12-01 15:57:06 來源:億速云 閱讀:242 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)利用javascript模仿一個(gè)快遞單號(hào)查詢功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

具體內(nèi)容如下

要求:當(dāng)我們?cè)谖谋究蛑休斎雰?nèi)容時(shí),文本框上面自動(dòng)顯示大字號(hào)的內(nèi)容

分析:

  • 輸入內(nèi)容時(shí),上面的大盒子會(huì)自動(dòng)顯示出來(這里字號(hào)更大)

  • 表單檢測(cè)用戶輸入,給表單添加鍵盤事件

  • 同時(shí)把快遞單號(hào)里面的值(value)獲取過來復(fù)制給大盒子作為內(nèi)容

  • 如果快遞單號(hào)里面內(nèi)容為空,就隱藏大盒子

  • 當(dāng)失去焦點(diǎn),大盒子也隱藏

注意:keydown 和 keypress 在文本框里面的特點(diǎn) : 他們兩個(gè)事件觸發(fā)的時(shí)候,文字還沒有落入文本框中,keyup 事件觸發(fā)的時(shí)候,文本已經(jīng)落入文本框里了

<style>
  * {
  padding: 0;
  margin: 0;
  }
  .search {
  position: relative;
  width: 178px;
  margin: 100px;
  }
  .con {
  display: none;
  position: absolute;
  top: -48px;
  width: 171px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  padding: 5px 0;
  font-size: 18px;
  line-height: 20px;
  color: #333;
  }
  .con::before {
  content: "";
  width: 0;
  height: 0;
  position: absolute;
  top: 28px;
  left: 18px;
  border: 8px solid #000;
  border-style: solid dashed dashed;
  border-color: #fff transparent transparent;
  }
 </style>
 </head>
 <body>
 <div class="search">
  <div class="con"></div>
  <input type="text" placeholder="請(qǐng)輸入您的快遞單號(hào)" class="jd" />
 </div>
 <script>
  var con = document.querySelector(".con");
  var jd_input = document.querySelector(".jd");
  jd_input.addEventListener("keyup", function () {
  if (this.value == "") {
   con.style.display = "none";
  } else {
   con.style.display = "block";
   con.innerHTML = this.value;
  }
  });
  //當(dāng)失去焦點(diǎn),就隱藏盒子
  jd_input.addEventListener("blur", function () {
  con.style.display = "none";
  });
  //當(dāng)獲得焦點(diǎn),就顯示盒子
  jd_input.addEventListener("focus", function () {
  if (this.value !== "") {
   con.style.display = "block";
  }
  });
 </script>
</body>

關(guān)于利用javascript模仿一個(gè)快遞單號(hào)查詢功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI