標籤:
一、什麼是DOM操作?
文件物件模型(Document Object Model,簡稱DOM),是W3C組織推薦的處理可擴充標誌語言的標準編程介面。在網頁上,組織頁面(或文檔)的對象被組織在一個樹形結構中,用來表示文檔中對象的標準模型就稱為DOM。Document Object Model的曆史可以追溯至1990年代後期微軟與Netscape的“瀏覽器大戰”,雙方為了在JavaScript與JScript一決生死,於是大規模的賦予瀏覽器強大的功能。微軟在網頁技術上加入了不少專屬事物,既有VBScript、ActiveX、以及微軟自家的DHTML格式等,使不少網頁使用非微軟平台及瀏覽器無法正常顯示。DOM即是當時蘊釀出來的傑作。
二、具體的解釋
Text()和html()的區別
也可以進行設定($(‘input‘).val(“Moshimol”));
當傳入參數進來是get方法,不傳參數是set方法
<html> <head> <title>JQuery DOM Manipulation</title> <link rel="stylesheet" href="style.css"> </head> <body> <input type="text"> <div class="hello">Hello World <p>I am Moshimol</p> </div> <button>Click me</button> </body> <script src="js/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(‘button‘).on(‘click‘, function() { //alert($(‘div‘).text()); //alert($(‘div‘).html()); alert($(‘input‘).val()); // $(‘div‘).replaceWith(‘<p>Hello World</p>‘); // $(‘.hello‘).before($(‘.world‘).clone()); // $(‘<p>Hello World</p>‘).replaceAll(‘div‘); }); }); </script></html>
## 擷取設定屬性 [attr(), removeAttr()]
$(‘div‘).removeAttr(‘class‘); 移除css
$(‘div‘).attr("class","hello") 擷取css
可以設定兩個
## 擷取設定 CSS Class [addClass(), removeClass(), hasClass(), toggleClass] hasclass 看有沒有這個class toggleClass 有就刪除 沒有就加上
## 擷取設定 CSS style [css()]可以列印div裡面的css 但是需要JSON.stringify這個函數來列印具體的,如果沒有這個函數的話,列印出options
## append() and prepend() methods append()把後面一個元素插入前面元素最後子項目中,一個向後面插 prepend這個是插入後面,一個向前面插
## appendTo() and prependTo() methods 和上面一樣 字面意思區別不是很大,但是前後位置倒反了
## before() and after() methods
## insertBefore() and insertAfter() methods
<html><head> <title>JQuery DOM Manipulation</title> <link rel="stylesheet" href="style.css"></head><body> <div class="a"> <div class="b">b</div> </div> <button>‘c‘</button> <div class="c">c</div><button>Click me</button></body><script src="js/jquery-latest.js"></script><script type="text/javascript"> $(document).ready(function(){ $(‘button‘).on(‘click‘, function() { // $(‘.a‘).append($(‘.c‘)); // $(‘.a‘).prepend($(‘.c‘)); //$(‘.c‘).appendTo($(‘.a‘)); //$(‘.c‘).prependTo($(‘.a‘)); //$(‘.a‘).after($(‘.c‘));插如之前 不是子項目 // $(‘.a‘).before($(‘.c‘));插入之後 不是子項目 $(‘.c‘).insertAfter($(‘.a‘)); $(‘.c‘).insertBefore($(‘.a‘)); }); });</script></html>
## remove(), empty() and .detach() methods
remove() 刪掉整個元素
empty() 使他變空 但是內容是存在的
.detach() 和remove 區別不是很大
可以自己實驗
var p; function selectChange() { if (document.getElementById("ddl_schoolarea").value != "請選擇") { p = $("#trlession").detach(); } else { //table1為一個table名字 $("#table1").append(p); } }
# unwrap(), wrapInner(), wrap(), wrapAll(), 逐層放大
<html><head> <title>JQuery DOM Manipulation</title> <link rel="stylesheet" href="style.css"></head><body> <div> <li>1</li> <li>2</li> <li>3</li> </div></body><script src="js/jquery-latest.js"></script><script type="text/javascript"> $(document).ready(function(){ $(‘button‘).on(‘click‘, function() { $(‘li‘).wrap("<ul></ul>>"); $(‘li‘).wrapAll("<ul></ul>>"); $(‘li‘).unwrap();$(‘li‘).wrapInner("<b></b>>"); }); });</script></html>
## target.replaceWith() and replaceAll(target)
<html><head> <title>JQuery DOM Manipulation</title> <link rel="stylesheet" href="style.css"></head><body> <div> <li>1</li> <li>2</li> <li>3</li> </div></body><script src="js/jquery-latest.js"></script><script type="text/javascript"> $(document).ready(function(){ $(‘button‘).on(‘click‘, function() { $(‘div‘).replaceWith(‘<p>Moshimol</p>‘); $ (‘<p>Moshimol</p>‘).replaceAll(‘div‘); }); });</script></html>
## clone() and Cloning Event Handlers and Data
clone()進行複製
$(‘.hello‘).before($(‘.world‘).clone());
相當於windows的拷貝功能,吧world的內容進行拷貝,再把他放到hello前面
jQuery學習教程(3)