jQuery實現單擊和滑鼠感應事件。

來源:互聯網
上載者:User

標籤:

1.實現單擊事件動態交替
http://www.cnblogs.com/ahthw/p/4232837.html講到了toggleClass(),對於單擊事件而言,jQuery同樣提供了動態交替的toggle()方法,這個方法接受兩個參數,兩個參數均為監聽函數,在click事件中交替使用。

例子:點擊事件的動態互動。

<script type="text/javascript">            $(function() {                $("#ddd").toggle(                    function(oEvent) {                        $(oEvent.target).css("opacity", "0.5");                    },                    function(oEvent) {                        $(oEvent.target).css("opacity", "1.0");                    }                );            });        </script>        <div id="ddd">11</div>

2.實現滑鼠感應

在css中可以利用:hover偽類實現樣式的改版,實現單獨的css樣式,在jQuery引入後,幾乎所有的元素都可以用hover()來感應滑鼠。並且可以製作更複雜的效果。其本質是mouseover和mouseout事件的合并
hover(over,out)方法可接受兩個參數,均為函數。第一個是滑鼠移動到元素上面觸發,第二個是滑鼠移出元素時觸發。

<script type="text/javascript">            $(function() {                $("#ddd").hover(                    function(oEvent) {                        //第一個函數相當於mouseover事件監聽                        $(oEvent.target).css("opacity", "0.5");                    },                    function(oEvent) {                        //第二個函數相當於mouseover事件監聽                        $(oEvent.target).css("opacity", "1.0");                    }                );            });        </script>        <div id="ddd">11</div>

與第一個例子相比,只是把toggle()換成了hover().

jQuery實現單擊和滑鼠感應事件。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.