国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

jQuery高級技巧——DOM操作篇

2024-04-27 15:00:40
字體:
來源:轉載
供稿:網友
 

頁面加載之DOMReady事件

所謂domReady,也就是文檔就緒,我們都知道,在操作dom時必須要在dom樹加載完成后才能進行操作。如何檢測DOM樹已經構建完成,以下是一些實現的方式:

1.使用jQuery:

// with jQuery$(document).ready(function(){ /* ... */ });// shorter jQuery version$(function(){ /* ... */ });

2.監聽DOMContentLoaded事件,DOM 樹創建完成后會觸發,不支持IE10以下版本。

// without jQuery (doesn't work in older IEs)document.addEventListener('DOMContentLoaded', function(){// your code goes here}, false);

3.監聽readyState狀態,可實現跨瀏覽器

readyState 的狀態屬性:

  • "uninitialized" – 原始狀態
  • "loading" – 下載數據中
  • "loaded" – 下載完成
  • "interactive" – 還未執行完畢
  • "complete" – 腳本執行完畢
r(function(){  alert('DOM Ready!');});function r(f){/in/.test(document.readyState)?setTimeout('r('+f+')',9):f()}

這個方法是不斷監聽readyState的loading狀態,加載完成后則執行對應方法。具體可參考:http://www.dustindiaz.com/smallest-domready-ever

根據特定頁面的執行對應的代碼

如果所有頁面的代碼都寫在一個javaScript文件中,這樣的代碼就會難以維護。簡單的辦法就是根據不同的頁面執行不同的代碼。來看下例子:

例如在test.js有以下代碼:

var route = {        _routes: {}, // The routes will be stored here        add: function(url, callback) {            this._routes[url] = callback;        },        run: function() {            jQuery.each(this._routes, function(pattern) { // pattern 指向_routes對象集合的key,即url                if (location.href.match(pattern)) {                    // "this" points to the function to be executed                    this(); //this 指向指向 _routes對象集合的value,即要執行的方法                }            });        }    }    // Will execute only on this page:route.add('test.html', function() {    alert('Hello there!');});route.add('function() {    alert("this won't be executed :(");});// You can even use regex-es:route.add('.*.html', function() {    alert('This is using a regex!');});route.run();

使用邏輯與運算符

利用邏輯與運算符可以簡化條件分支語句寫法,例子:

一般的寫法:

更好的寫法:

非常有用的jquery is()方法

is()方法非常有用,來看些例子:

HTML:

JS:

定義一個exists函數

判斷一個jQuery對象是否存在需要判斷length屬性,可以封裝為exists函數,簡化代碼,更加易讀。

HTML:

JS:

使用$()函數的第二個參數

$()函數可以接收兩個參數,第二個參數的作用是什么,可以來看下例子:

作用一:

作用二:

//創建一個元素。第二個參數為對應的配置屬性,包含jQuery方法會被執行var div = $('<div>' ,{ "class" : "bigBlue" , "CSS" : { "background-color" : "purple" }, "width" : 20, "height" : 20, "animate" : { //使用jQuery的方法作為屬性 "width" : 200, "height" : 50 }});div. appendTo('body' );

取消右鍵Click事件

$(function(){    $(document).on("contextmenu" , function(e){             e. preventDefault();    });});

取消文本選中

//適應于所有瀏覽器
$('p.descr' ). attr('unselectable' , 'on' ) . css('user-select' , 'none' ) . on('selectstart' , false);

解析錨元素URL

// 需要解析的URLvar url = 'http://tutorialzine.com/books/jquery-trickshots?trick=12#comments' ;// 通過url創建一個新的鏈接var a = $('<a>' ,{ href:  url });console. log('Host name: ' + a. prop('hostname' ));console. log('Path: ' + a. prop('pathname' ));console. log('Query: ' + a. prop('search' ));console. log('Protocol: ' + a. prop('protocol' ));console. log('Hash: ' + a. prop('hash' ));

輸出結果:

Host name: tutorialzine.com
Path: /books/jquery-trickshots
Query: ?trick=12
Protocol: http:
Hash: #comments

以上是一些知識總結,如有任何建議或疑問,歡迎留言討論。

參考鏈接:

http://www.cnblogs.com/rubylouvre/p/4277408.html

http://www.dustindiaz.com/smallest-domready-ever


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚顺县| 巧家县| 蓬莱市| 浑源县| 襄汾县| 永康市| 资溪县| 钟山县| 萍乡市| 章丘市| 贵定县| 中西区| 广汉市| 通化县| 察隅县| 扎囊县| 东丽区| 长顺县| 乐亭县| 民丰县| 泰兴市| 江门市| 延川县| 日喀则市| 古蔺县| 永和县| 肥西县| 孝昌县| 留坝县| 宜宾县| 湾仔区| 横山县| 三门峡市| 兴业县| 慈溪市| 叶城县| 曲周县| 贡觉县| 金秀| 舟曲县| SHOW|