Jqyery中同等與js中windows.onload的應用

來源:互聯網
上載者:User

複製代碼 代碼如下:$(function(){//指令碼})
Jquery(function(){//指令碼})
Jquery(document).ready(function(){//指令碼})

以上三個代碼執行同一個動作,由於書寫方便,所以Jquery比其他應用程式更受歡迎,但是當與其他js程式庫一起使用的時候,特別是prototype.js同時使用時,不能使用$方法,因為他們也有該方法,可以用如下方法迴避這種衝突:

jQuery(function($){//指令碼})

他們都是當Dom載入完後執行的操作,所以放在html的任何位置都可以,但是有些情況,比如上一節提到的載入事件的方法 複製代碼 代碼如下:<div id="divInfo">Hello,Word!</div>
<input id="btnShow" type="button" value="顯示" />
<input id="btnHid" type="button" value="隱藏" />
<input id="btnChange" type="button" value="修改為:hello" />
<script type="text/javascript">
$("#btnShow").bind("click",function(event){$("#divInfo").show()});
$("#btnHid").bind("click",function(event){$("#divInfo").hide();});
$("#btnChange").bind("click",function(event){$("#divInfo").html("hello");});
</script>

這裡面則是在載入的過程中執行的,就是必須載入完相關元素才可以執行,如果把他們放到上面的代碼中就靈活的多了 複製代碼 代碼如下:<div id="divInfo">Hello,Word!</div>
<input id="btnShow" type="button" value="顯示" />
<input id="btnHid" type="button" value="隱藏" />
<input id="btnChange" type="button" value="修改為:hello" />
<script type="text/javascript">
jQuery(function($){$("#btnShow").bind("click",function(event){$("#divInfo").show()});
$("#btnHid").bind("click",function(event){$("#divInfo").hide();});
$("#btnChange").bind("click",function(event){$("#divInfo").html("hello");})
})
</script>

那麼這個載入事件可以放在任何位置了...

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.