首頁 web前端 js教程 javascript 按鍵事件(相容各瀏覽器)_javascript技巧

javascript 按鍵事件(相容各瀏覽器)_javascript技巧

May 16, 2016 pm 05:08 PM
javascript

第一部分:瀏覽器的按鍵事件

用js實作鍵盤記錄,要注意瀏覽器的三種按鍵事件類型,即keydown,keypress和keyup,它們分別對應onkeydown、onkeypress和onkeyup這三個事件句柄。典型的按鍵會產生這三種事件,依序是keydown,keypress,然後是按鍵釋放時候的keyup。

在這3種事件類型中,keydown和keyup比較底層,而keypress比較進階。這裡所謂的高級是指,當用戶按下shift 1時,keypress是對這個按鍵事件進行解析後返回一個可打印的“!”字符,而keydown和keyup只是記錄了shift 1這個事件。 [1]

但是keypress只能針對一些可以列印出來的字元有效,而對於功能按鍵,如F1-F12、Backspace、Enter、Escape、PageUP、PageDown和箭頭方向等,就不會產生keypress事件,但可以產生keydown和keyup事件。然而在FireFox中,功能按鍵是可以產生keypress事件的。

傳遞給keydown、keypress和keyup事件句柄的事件物件有一些通用的屬性。如果Alt、Ctrl或Shift和一個按鍵一起按下,這透過事件的altKey、ctrlKey和shiftKey屬性表示,這些屬性在FireFox和IE中是通用的。

第二部分:相容瀏覽器

凡是涉及瀏覽器的js,就都要考慮瀏覽器相容的問題。
目前常用的瀏覽器主要有基於IE和基於Mozilla兩大類。 Maxthon是基於IE內核的,而FireFox和Opera是基於Mozilla內核的。

2.1 事件的初始化

首先需要了解的是如何初始化該事件,基本語句如下:

   function keyDown(){}
   document.onkeydown = keyDown;

當瀏覽器讀到這個語句時,無論按下鍵盤上的哪個鍵,都會呼叫KeyDown()函數。

2.2 FireFox和Opera的實作方法

FireFox和Opera等程式實作要比IE麻煩,所以這裡先描述一下。

keyDown()函數有一個隱藏的變數--一般的,我們使用字母「e」來表示這個變數。

   function keyDown(e)

變數e表示發生擊鍵事件,尋找是哪個鍵被按下,要使用which這個屬性:

   e.which

e.which將給出該鍵的索引值,將索引值轉換成該鍵的字母或數字值的方法需要用到靜態函數String.fromCharCode(),如下:

   String.fromCharCode(e.which)

把上面的語句放在一起,我們可以在FireFox中得到被按下的是哪一個鍵:

複製程式碼


程式碼如下:


   function keyDown(e) {
      var keycode = e.which;  〜〜);      alert( "按鍵碼: " keycode " 字元: " realkey);
   }
   document.onkeydown = keyDown;
2.3 IE的實作方法


2.3 IE的實作方法IE的程式不需要e變量,用window.event.keyCode來取代e.which,把鍵的索引值轉換為真實鍵值方法類似:String.fromCharCode(event.keyCode),程式如下:
複製程式碼


程式碼如下:


   function  ;
      var realkey = String.fromCharCode(event.keyCode);
      alert("按鍵碼: " keycode "   realkey); 🎜>

2.4 判斷瀏覽器類型

上面了解了在各種瀏覽器裡是如何實現獲取按鍵事件對象的方法,那麼下面需要判斷瀏覽器類型,這個方法很多,有比較方便理解的,也有很巧妙的辦法,先說一般的方法:就是利用navigator物件的appName屬性,當然也可以用userAgent屬性,這裡用appName來實作判斷瀏覽器類型,IE和Maxthon的appName是“Microsoft Internet Explorer” ,而FireFox和Opera的appName是“Netscape”,所以一個功能比較簡單的程式碼如下:

複製程式碼 程式碼如下:

   function keyUp(e) {
      if(navigator.appName == "Microsoft Internet Explorer")
         🎜>        var realkey = String. fromCharCode(event.keyCode);
      }else
      {
         var  key = String.fromCharCode(e.which);
      }
      alert( "按鍵碼: " keycode " 字元: " realkey);
    }
   document.onkeyup = keyUp;


比較簡潔的方法是[2]

比較簡潔的方法是[2]
複製程式碼 程式碼如下:
   function Up(e) { =c,> |event;
     currKey=e.keyCode||e.which||e.charCode;
     var keyName = String.fromCharCode(currKey);    }
   document.onkeyup = keyUp;


上面這種方法比較巧妙,簡單地解釋一下:
首先,e=e||event;句代碼是為了進行瀏覽器事件物件取得的相容。 js中這句程式碼的意思是,如果在FireFox或Opera中,隱藏的變數e是存在的,那麼e||event回傳e,如果在IE中,隱藏變數e是不存在,則傳回event。
其次,currKey=e.keyCode||e.which||e.charCode;這句話是為了相容瀏覽器按鍵事件物件的按鍵碼屬性(詳見第三部分),如IE中,只有keyCode屬性,而FireFox中有which和charCode屬性,Opera中有keyCode和which屬性等。

上述程式碼只是相容了瀏覽器,取得了keyup事件對象,簡單的彈出了按鍵碼和按鍵的字符,但是問題出現了,當你按鍵時,字符鍵都是大寫的,而按shift鍵時,顯示的字元很奇怪,所以就需要優化一下程式碼了。

第三部分:程式碼實作與最佳化

3.1 按鍵事件的按鍵碼和字元碼

按鍵事件的按鍵碼和字元碼缺乏瀏覽器間的可移植性,對於不同的瀏覽器和不同的案件事件,按鍵碼和字元碼的儲存方式都是不同的,...... .在IE中,只有一個keyCode屬性,並且它的解釋取決於事件類型。對於keydown來說,keyCode儲存的是按鍵碼,對於keypress事件來說,keyCode儲存的是一個字元碼。而IE中沒有which和charCode屬性,所以which和charCode屬性總是undefined。

FireFox中keyCode總是0,時間keydown/keyup時,charCode=0,which為按鍵碼。事件keypress時,which和charCode二者的值相同,儲存了字元碼。


在Opera中,keyCode和which二者的值始終相同,在keydown/keyup事件中,它們儲存按鍵碼,在keypress時間中,它們儲存字元碼,而charCode沒有定義,始終是undefined。

3.2 用keydown/keyup還是keypress

第一部分已經介紹了keydown/keyup和keypress的區別,有一條比較通用的規則,keydown事件對於功能按鍵來說是最有用的,而keypress事件對於可列印按鍵來說是最有用的[3 ]。 鍵盤記錄主要是針對於可列印字元和部分功能按鍵,所以keypress是首選,然而正如第一部分提到的,IE中keypress不支援功能按鍵,所以應該用keydown/keyup事件來進行補充。

3.3 程式碼的實作

整體思路,用keypress事件物件取得按鍵字符,用keydown事件取得功能字符,如Enter,Backspace等。

程式碼實作如下


複製程式碼

程式碼如下:

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

js キーストローク ロギング< TITLE> <br><META NAME="ジェネレーター" CONTENT="EditPlus"> <br><META NAME="著者" CONTENT="Yu Shangren"> CONTENT="js キーストローク ログ"> <br><META NAME="説明" CONTENT="js キーストローク ログ"> <br><BODY> type="text/javascript"> <br>var keystring = "";//レコードキー文字列<br>function $(s){return document.getElementById(s)?document.getElementById(s) :s;} <br>関数 keypress(e) <br>{ <br> var currKey=0,CapsLock=0,e=e||event; <br> currKey=e.keyCode||e.what|| e.charCode; <br> CapsLock=currKey>=65&&currKey switch(currKey) <br> { <br> // バックスペース、タブ、キャリッジ リターン、スペース、矢印キー、削除はブロックされます Key<br> の場合8: ケース 9: ケース 13: ケース 32: ケース 37: ケース 38: ケース 40: ケース 46: keyName = "";break; <br> デフォルト:keyName = String.fromCharCode(KeycurrKey); 🎜> } <br> keystring = keyName; <br>} <br>関数 keydown(e) <br> var e=e||event; <br> var currKey=e |どの | 🎜> ケース 8: keyName = "[Backspace]" ブレーク <br> ケース 13: keyName = "[Enter];ブレーク; <br> ケース 32:keyName = "[スペース]"; ブレーク <br> ケース 34:キー名 = "[ページダウン]"; <br> case 35:keyName = "[終了]"; <br> case 36:keyName = "[ホーム]"; <br> case 37:keyName = "[矢印キー]"; <br> case 38:keyName = "[方向キーは上]"; <br> case 39:keyName = "[方向キーは右]"; <br> case 40:keyName = "[方向キーは下]; "; ブレーク; <br> case 46:keyName = "[削除]"; ブレーク; <br> default:keyName = ""; ブレーク; <br> } <br> keystring = keyName; "content").innerHTML= keystring; <br>} <br>function keyup(e) <br>{ <br> $("content").innerHTML=keystring; <br>document.onkeypress= keypress; onkeydown =keydown; <br></script> <br><input type= "button" value="クリアRecord" onclick="$('content').innerHTML = '';keystring = '';"/> <br><br/> キーボードの応答を表示するには、任意のキーを押してください。キーの値: <span id="content"></span> <br></BODY> <br><br><br>コード分析: <br>$ (): dom ベースの取得ID <br><br>keypress(e): ファンクション キーは keydown を使用して取得する必要があるため、keypress でブロックされます。 <br><br>keydown(e): 主にファンクションキーの取得を実現します。 <br><br>keyup(e): インターセプトされた文字列を表示します。 <br> </div></u></a></span></div> </div> </div> <div class="wzconShengming_sp"> <div class="bzsmdiv_sp">本網站聲明</div> <div>本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn</div> </div> </div> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="2507867629"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div class="AI_ToolDetails_main4sR"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5902227090019525" data-ad-slot="3653428331" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <!-- <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>熱門文章</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796789525.html" title="Windows 11 KB5054979中的新功能以及如何解決更新問題" class="phpgenera_Details_mainR4_bottom_title">Windows 11 KB5054979中的新功能以及如何解決更新問題</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793874.html" title="如何修復KB5055523無法在Windows 11中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055523無法在Windows 11中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796787760.html" title="Inzoi:如何申請學校和大學" class="phpgenera_Details_mainR4_bottom_title">Inzoi:如何申請學校和大學</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793871.html" title="如何修復KB5055518無法在Windows 10中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055518無法在Windows 10中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796786383.html" title="Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla" class="phpgenera_Details_mainR4_bottom_title">Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By 尊渡假赌尊渡假赌尊渡假赌</span> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/article.html">顯示更多</a> </div> </div> </div> --> <div class="phpgenera_Details_mainR3"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hottools2.png" alt="" /> <h2>熱AI工具</h2> </div> <div class="phpgenera_Details_mainR3_bottom"> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411540686492.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undresser.AI Undress" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_title"> <h3>Undresser.AI Undress</h3> </a> <p>人工智慧驅動的應用程序,用於創建逼真的裸體照片</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411552797167.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="AI Clothes Remover" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_title"> <h3>AI Clothes Remover</h3> </a> <p>用於從照片中去除衣服的線上人工智慧工具。</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173410641626608.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undress AI Tool" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_title"> <h3>Undress AI Tool</h3> </a> <p>免費脫衣圖片</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411529149311.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Clothoff.io" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_title"> <h3>Clothoff.io</h3> </a> <p>AI脫衣器</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173414504068133.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Video Face Swap" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_title"> <h3>Video Face Swap</h3> </a> <p>使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!</p> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/ai">顯示更多</a> </div> </div> </div> <script src="https://sw.php.cn/hezuo/cac1399ab368127f9b113b14eb3316d0.js" type="text/javascript"></script> <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>熱門文章</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796789525.html" title="Windows 11 KB5054979中的新功能以及如何解決更新問題" class="phpgenera_Details_mainR4_bottom_title">Windows 11 KB5054979中的新功能以及如何解決更新問題</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793874.html" title="如何修復KB5055523無法在Windows 11中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055523無法在Windows 11中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796787760.html" title="Inzoi:如何申請學校和大學" class="phpgenera_Details_mainR4_bottom_title">Inzoi:如何申請學校和大學</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793871.html" title="如何修復KB5055518無法在Windows 10中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055518無法在Windows 10中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796786383.html" title="Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla" class="phpgenera_Details_mainR4_bottom_title">Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By 尊渡假赌尊渡假赌尊渡假赌</span> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/article.html">顯示更多</a> </div> </div> </div> <div class="phpgenera_Details_mainR3"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hottools2.png" alt="" /> <h2>熱工具</h2> </div> <div class="phpgenera_Details_mainR3_bottom"> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/92" title="記事本++7.3.1" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab96f0f39f7357.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="記事本++7.3.1" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/92" title="記事本++7.3.1" class="phpmain_tab2_mids_title"> <h3>記事本++7.3.1</h3> </a> <p>好用且免費的程式碼編輯器</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/93" title="SublimeText3漢化版" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab97a3baad9677.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3漢化版" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/93" title="SublimeText3漢化版" class="phpmain_tab2_mids_title"> <h3>SublimeText3漢化版</h3> </a> <p>中文版,非常好用</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/121" title="禪工作室 13.0.1" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab97ecd1ab2670.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="禪工作室 13.0.1" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/121" title="禪工作室 13.0.1" class="phpmain_tab2_mids_title"> <h3>禪工作室 13.0.1</h3> </a> <p>強大的PHP整合開發環境</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58d0e0fc74683535.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Dreamweaver CS6" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_title"> <h3>Dreamweaver CS6</h3> </a> <p>視覺化網頁開發工具</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/500" title="SublimeText3 Mac版" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58d34035e2757995.png?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3 Mac版" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/500" title="SublimeText3 Mac版" class="phpmain_tab2_mids_title"> <h3>SublimeText3 Mac版</h3> </a> <p>神級程式碼編輯軟體(SublimeText3)</p> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/ai">顯示更多</a> </div> </div> </div> <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>熱門話題</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/gmailyxdlrkzn" title="gmail信箱登陸入口在哪裡" class="phpgenera_Details_mainR4_bottom_title">gmail信箱登陸入口在哪裡</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>7843</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>15</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/java-tutorial" title="Java教學" class="phpgenera_Details_mainR4_bottom_title">Java教學</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1649</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>14</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/cakephp-tutor" title="CakePHP 教程" class="phpgenera_Details_mainR4_bottom_title">CakePHP 教程</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1403</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>52</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/laravel-tutori" title="Laravel 教程" class="phpgenera_Details_mainR4_bottom_title">Laravel 教程</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1300</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>25</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/php-tutorial" title="PHP教程" class="phpgenera_Details_mainR4_bottom_title">PHP教程</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1240</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>29</span> </div> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/faq/zt">顯示更多</a> </div> </div> </div> </div> </div> <div class="Article_Details_main2"> <div class="phpgenera_Details_mainL4"> <div class="phpmain1_2_top"> <a href="javascript:void(0);" class="phpmain1_2_top_title">Related knowledge<img src="/static/imghw/index2_title2.png" alt="" /></a> </div> <div class="phpgenera_Details_mainL4_info"> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/633424.html" title="如何使用WebSocket和JavaScript實現線上語音辨識系統" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/000/887/227/170279609162144.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="如何使用WebSocket和JavaScript實現線上語音辨識系統" /> </a> <a href="https://www.php.cn/zh-tw/faq/633424.html" title="如何使用WebSocket和JavaScript實現線上語音辨識系統" class="phphistorical_Version2_mids_title">如何使用WebSocket和JavaScript實現線上語音辨識系統</a> <span class="Articlelist_txts_time">Dec 17, 2023 pm 02:54 PM</span> <p class="Articlelist_txts_p">如何使用WebSocket和JavaScript實現線上語音辨識系統引言:隨著科技的不斷發展,語音辨識技術已成為了人工智慧領域的重要組成部分。而基於WebSocket和JavaScript實現的線上語音辨識系統,具備了低延遲、即時性和跨平台的特點,成為了廣泛應用的解決方案。本文將介紹如何使用WebSocket和JavaScript來實現線上語音辨識系</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/633480.html" title="WebSocket與JavaScript:實現即時監控系統的關鍵技術" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/000/465/014/170280543760094.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="WebSocket與JavaScript:實現即時監控系統的關鍵技術" /> </a> <a href="https://www.php.cn/zh-tw/faq/633480.html" title="WebSocket與JavaScript:實現即時監控系統的關鍵技術" class="phphistorical_Version2_mids_title">WebSocket與JavaScript:實現即時監控系統的關鍵技術</a> <span class="Articlelist_txts_time">Dec 17, 2023 pm 05:30 PM</span> <p class="Articlelist_txts_p">WebSocket與JavaScript:實現即時監控系統的關鍵技術引言:隨著互聯網技術的快速發展,即時監控系統在各個領域中得到了廣泛的應用。而實現即時監控的關鍵技術之一就是WebSocket與JavaScript的結合使用。本文將介紹WebSocket與JavaScript在即時監控系統中的應用,並給出程式碼範例,詳細解釋其實作原理。一、WebSocket技</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/633365.html" title="如何利用JavaScript和WebSocket實現即時線上點餐系統" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/000/887/227/170278617570921.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="如何利用JavaScript和WebSocket實現即時線上點餐系統" /> </a> <a href="https://www.php.cn/zh-tw/faq/633365.html" title="如何利用JavaScript和WebSocket實現即時線上點餐系統" class="phphistorical_Version2_mids_title">如何利用JavaScript和WebSocket實現即時線上點餐系統</a> <span class="Articlelist_txts_time">Dec 17, 2023 pm 12:09 PM</span> <p class="Articlelist_txts_p">如何利用JavaScript和WebSocket實現即時線上點餐系統介紹:隨著網路的普及和技術的進步,越來越多的餐廳開始提供線上點餐服務。為了實現即時線上點餐系統,我們可以利用JavaScript和WebSocket技術。 WebSocket是一種基於TCP協定的全雙工通訊協議,可實現客戶端與伺服器的即時雙向通訊。在即時線上點餐系統中,當使用者選擇菜餚並下訂單</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/633311.html" title="如何使用WebSocket和JavaScript實現線上預約系統" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/000/887/227/170277714783917.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="如何使用WebSocket和JavaScript實現線上預約系統" /> </a> <a href="https://www.php.cn/zh-tw/faq/633311.html" title="如何使用WebSocket和JavaScript實現線上預約系統" class="phphistorical_Version2_mids_title">如何使用WebSocket和JavaScript實現線上預約系統</a> <span class="Articlelist_txts_time">Dec 17, 2023 am 09:39 AM</span> <p class="Articlelist_txts_p">如何使用WebSocket和JavaScript實現線上預約系統在當今數位化的時代,越來越多的業務和服務都需要提供線上預約功能。而實現一個高效、即時的線上預約系統是至關重要的。本文將介紹如何使用WebSocket和JavaScript來實作一個線上預約系統,並提供具體的程式碼範例。一、什麼是WebSocketWebSocket是一種在單一TCP連線上進行全雙工</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/633476.html" title="JavaScript與WebSocket:打造高效率的即時天氣預報系統" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/000/465/014/170280442251580.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="JavaScript與WebSocket:打造高效率的即時天氣預報系統" /> </a> <a href="https://www.php.cn/zh-tw/faq/633476.html" title="JavaScript與WebSocket:打造高效率的即時天氣預報系統" class="phphistorical_Version2_mids_title">JavaScript與WebSocket:打造高效率的即時天氣預報系統</a> <span class="Articlelist_txts_time">Dec 17, 2023 pm 05:13 PM</span> <p class="Articlelist_txts_p">JavaScript和WebSocket:打造高效的即時天氣預報系統引言:如今,天氣預報的準確性對於日常生活以及決策制定具有重要意義。隨著技術的發展,我們可以透過即時獲取天氣數據來提供更準確可靠的天氣預報。在本文中,我們將學習如何使用JavaScript和WebSocket技術,來建立一個高效的即時天氣預報系統。本文將透過具體的程式碼範例來展示實現的過程。 We</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/642543.html" title="簡易JavaScript教學:取得HTTP狀態碼的方法" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/000/887/227/170444932683164.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="簡易JavaScript教學:取得HTTP狀態碼的方法" /> </a> <a href="https://www.php.cn/zh-tw/faq/642543.html" title="簡易JavaScript教學:取得HTTP狀態碼的方法" class="phphistorical_Version2_mids_title">簡易JavaScript教學:取得HTTP狀態碼的方法</a> <span class="Articlelist_txts_time">Jan 05, 2024 pm 06:08 PM</span> <p class="Articlelist_txts_p">JavaScript教學:如何取得HTTP狀態碼,需要具體程式碼範例前言:在Web開發中,經常會涉及到與伺服器進行資料互動的場景。在與伺服器進行通訊時,我們經常需要取得傳回的HTTP狀態碼來判斷操作是否成功,並根據不同的狀態碼來進行對應的處理。本篇文章將教你如何使用JavaScript來取得HTTP狀態碼,並提供一些實用的程式碼範例。使用XMLHttpRequest</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/630681.html" title="javascript如何使用insertBefore" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="?x-oss-process=image/resize,m_fill,h_207,w_330" alt="javascript如何使用insertBefore" /> </a> <a href="https://www.php.cn/zh-tw/faq/630681.html" title="javascript如何使用insertBefore" class="phphistorical_Version2_mids_title">javascript如何使用insertBefore</a> <span class="Articlelist_txts_time">Nov 24, 2023 am 11:56 AM</span> <p class="Articlelist_txts_p">用法:在JavaScript中,insertBefore()方法用於在DOM樹中插入一個新的節點。這個方法需要兩個參數:要插入的新節點和參考節點(即新節點將要插入的位置的節點)。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/633281.html" title="JavaScript與WebSocket:打造高效率的即時影像處理系統" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/000/887/227/170277372169688.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="JavaScript與WebSocket:打造高效率的即時影像處理系統" /> </a> <a href="https://www.php.cn/zh-tw/faq/633281.html" title="JavaScript與WebSocket:打造高效率的即時影像處理系統" class="phphistorical_Version2_mids_title">JavaScript與WebSocket:打造高效率的即時影像處理系統</a> <span class="Articlelist_txts_time">Dec 17, 2023 am 08:41 AM</span> <p class="Articlelist_txts_p">JavaScript是一種廣泛應用於Web開發的程式語言,而WebSocket則是一種用於即時通訊的網路協定。結合二者的強大功能,我們可以打造一個高效率的即時影像處理系統。本文將介紹如何利用JavaScript和WebSocket來實作這個系統,並提供具體的程式碼範例。首先,我們需要明確指出即時影像處理系統的需求和目標。假設我們有一個攝影機設備,可以擷取即時的影像數</p> </div> </div> <a href="https://www.php.cn/zh-tw/web-designer.html" class="phpgenera_Details_mainL4_botton"> <span>See all articles</span> <img src="/static/imghw/down_right.png" alt="" /> </a> </div> </div> </div> </main> <footer> <div class="footer"> <div class="footertop"> <img src="/static/imghw/logo.png" alt=""> <p>公益線上PHP培訓,幫助PHP學習者快速成長!</p> </div> <div class="footermid"> <a href="https://www.php.cn/zh-tw/about/us.html">關於我們</a> <a href="https://www.php.cn/zh-tw/about/disclaimer.html">免責聲明</a> <a href="https://www.php.cn/zh-tw/update/article_0_1.html">Sitemap</a> </div> <div class="footerbottom"> <p> © php.cn All rights reserved </p> </div> </div> </footer> <input type="hidden" id="verifycode" value="/captcha.html"> <script>layui.use(['element', 'carousel'], function () {var element = layui.element;$ = layui.jquery;var carousel = layui.carousel;carousel.render({elem: '#test1', width: '100%', height: '330px', arrow: 'always'});$.getScript('/static/js/jquery.lazyload.min.js', function () {$("img").lazyload({placeholder: "/static/images/load.jpg", effect: "fadeIn", threshold: 200, skip_invisible: false});});});</script> <script src="/static/js/common_new.js"></script> <script type="text/javascript" src="/static/js/jquery.cookie.js?1745979815"></script> <script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script> <link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css?2' type='text/css' media='all' /> <script type='text/javascript' src='/static/js/viewer.min.js?1'></script> <script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script> <script type="text/javascript" src="/static/js/global.min.js?5.5.53"></script> <script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function () { var u = "https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u + 'matomo.php']); _paq.push(['setSiteId', '9']); var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0]; g.async = true; g.src = u + 'matomo.js'; s.parentNode.insertBefore(g, s); })(); </script> <script> // top layui.use(function () { var util = layui.util; util.fixbar({ on: { mouseenter: function (type) { layer.tips(type, this, { tips: 4, fixed: true, }); }, mouseleave: function (type) { layer.closeAll("tips"); }, }, }); }); document.addEventListener("DOMContentLoaded", (event) => { // 定义一个函数来处理滚动链接的点击事件 function setupScrollLink(scrollLinkId, targetElementId) { const scrollLink = document.getElementById(scrollLinkId); const targetElement = document.getElementById(targetElementId); if (scrollLink && targetElement) { scrollLink.addEventListener("click", (e) => { e.preventDefault(); // 阻止默认链接行为 targetElement.scrollIntoView({ behavior: "smooth" }); // 平滑滚动到目标元素 }); } else { console.warn( `Either scroll link with ID '${scrollLinkId}' or target element with ID '${targetElementId}' not found.` ); } } // 使用该函数设置多个滚动链接 setupScrollLink("Article_Details_main1L2s_1", "article_main_title1"); setupScrollLink("Article_Details_main1L2s_2", "article_main_title2"); setupScrollLink("Article_Details_main1L2s_3", "article_main_title3"); setupScrollLink("Article_Details_main1L2s_4", "article_main_title4"); setupScrollLink("Article_Details_main1L2s_5", "article_main_title5"); setupScrollLink("Article_Details_main1L2s_6", "article_main_title6"); // 可以继续添加更多的滚动链接设置 }); window.addEventListener("scroll", function () { var fixedElement = document.getElementById("Article_Details_main1Lmain"); var scrollTop = window.scrollY || document.documentElement.scrollTop; // 兼容不同浏览器 var clientHeight = window.innerHeight || document.documentElement.clientHeight; // 视口高度 var scrollHeight = document.documentElement.scrollHeight; // 页面总高度 // 计算距离底部的距离 var distanceToBottom = scrollHeight - scrollTop - clientHeight; // 当距离底部小于或等于300px时,取消固定定位 if (distanceToBottom <= 980) { fixedElement.classList.remove("Article_Details_main1Lmain"); fixedElement.classList.add("Article_Details_main1Lmain_relative"); } else { // 否则,保持固定定位 fixedElement.classList.remove("Article_Details_main1Lmain_relative"); fixedElement.classList.add("Article_Details_main1Lmain"); } }); </script> <script> document.addEventListener('DOMContentLoaded', function() { const mainNav = document.querySelector('.Article_Details_main1Lmain'); const header = document.querySelector('header'); if (mainNav) { window.addEventListener('scroll', function() { const scrollPosition = window.scrollY; if (scrollPosition > 84) { mainNav.classList.add('fixed'); } else { mainNav.classList.remove('fixed'); } }); } }); </script> </body> </html>