<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                剛開始以為this和`$(this)`就是一模子刻出來。但是我在閱讀時,和coding時發現,總不是一回事,這里就談談this與$(this)的區別。 ### 1、jQuery中this與$(this)的區別 ~~~ $("#textbox").hover( function() { this.title = "Test"; }, fucntion() { this.title = "OK”; } ); ~~~ 這里的this其實是一個html 元素(textbox),textbox有text屬性,所以這樣寫是完全沒有什么問題的。 但是如果將this換成`$(this)`就不是那回事了,報Error了。this與 $(this)的區別在此。 ~~~ \\Error Code: $("#textbox").hover( function() { $(this).title = "Test"; }, function() { $(this).title = "OK"; } ); ~~~ $()這是jQuery的一個函數,也是最核心最基本的函數 功能一:傳入一個選擇器字符串,獲得這個選擇器對應的dom內容,保存在[]中,也就是俗稱的jQuery對象。例如 ~~~ $('#id') $('.class') $('tag') ~~~ 功能二:傳入一個匿名函數,例如 ~~~ $(function(){})//這個匿名函數在網頁載入完成后開始執行 ~~~ 功能三:將javascript對象包裝成為jQuery對象。例如 ~~~ $(this) $({a:1,b:2,c:3}) $(document.getElementById('idstr')) ~~~ this是javascript**自身的**語法關鍵字,它指向一個javascript對象,所以可以使用所指向的目標javascript對象所擁有的方法, 但他自己不是一個普通的變量,所以你無法自己定義一個變量叫this 所以為了使用jQuery對象的方法,你必須傳入jQuery函數$(this), 將javascript 對象包裝成為一個jquery對象。 這里的$(this)是一個JQuery對象,而jQuery對象沒有title 屬性,因此這樣寫是錯誤的。 JQuery擁有attr()方法可以get/set DOM對象的屬性,所以正確的寫法應該是這樣: 正確的代碼: ~~~ $("#textbox").hover( function() { $(this).attr(’title’, ‘Test’); }, function() { $(this).attr(’title’, ‘OK’); } ); ~~~ 使用jQuery的好處是它包裝了各種瀏覽器版本對DOM對象的操作,因此統一使用$(this)而不再用this應該是比較不錯的選擇。 jQuery中this與$(this)的區別就介紹到這里。 ### 2、典型錯誤與注意點 ~~~ var node = $('#id'); node.click(function(){   this.css('display','block');  //報錯 this是一個html元素,不是jquery對象,因此this不能調用jquery 的css()方法   $(this).css();      //正確   $(this)是一個jquery對象,不是html元素,可以用css()方法   this.style.display = 'block';  //正確 this是一個html元素,不是jquery對象,因此this不能調用jquery的css()方法,但是可以用javascript來更改style屬性 }); ~~~ **不要濫用$(this)** 如果不了解javasrcipt中基本的DOM屬性和方法的話,很容易濫用jQuery對象。比如: ~~~ $(‘#someAnchor’).click(function() { alert( $(this).attr(‘id’) ); }); ~~~ 如果你只是通過jQ對象獲取簡單的dom元素的屬性比如id,那么你完全可以使用js原生的方法: ~~~ $(‘#someAnchor’).click(function() { alert( this.id ); }); ~~~ 諸如“src,” “href,” 和“style.”等一些屬性在老版本的ie中使用了getAttribute方法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看