獲得内容和屬性:
jQuery 擁有可操作 HTML 元素和屬性的強大方法。
jQuery DOM操作:
獲得内容:
三個簡單實用的用于 DOM 操作的 jQuery 方法:
- text() - 設定或傳回所選元素的文本内容
- html() - 設定或傳回所選元素的内容(包括 HTML 标記)
- val() - 設定或傳回表單字段的值,獲得輸入字段的值
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn1").click(function(){
alert("Test:"+$("#test").text());
});
$("#btn2").click(function(){
alert("html:"+$("#test").html());
});
})
</script>
<p id="test">這是段落中的<b>粗體</b>文本。</p>
<button id="btn1">顯示文本</button>
<button id="btn2">顯示 HTML</button>
擷取屬性:attr() 用于擷取屬性值
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
alert("href值:"+$("#w3s").attr("href"));
});
})
</script>
</head>
<body>
<p><a href="http://www.w3school.com.cn" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" id="w3s">W3School.com.cn</a></p>
<button>顯示 href 值</button>
</body>
設定内容和屬性:
設定内容 - text()、html() 以及 val()
script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text("hello world");
});
$("#btn2").click(function(){
$("#test2").html("<b>hello world</b>");
});
$("#btn3").click(function(){
$("#test3").val("Doolu");
});
})
</script>
</head>
<body>
<p id="test1">這是段落。</p>
<p id="test2">這是另一個段落。</p>
<p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p>
<button id="btn1">設定文本</button>
<button id="btn2">設定 HTML</button>
<button id="btn3">設定值</button>
text()、html() 以及 val() 的回調函數:
text()、html() 以及 val(),同樣擁有回調函數。回調函數由兩個參數:
- 被選元素清單中目前元素的下标 i
- 原始(舊的)值。 origTest
然後以函數新值傳回您希望使用的字元串。
目前元素的下标,原來是一組相同的class時目前文本的下标:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn1").click(function(){
$(".aa").text(function(i,oriText){
alert("原文本:"+oriText+"新文本:hello world! index("+i+")");
});
});
});
</script>
</head>
<body>
<button id="btn1">測試下标</button>
<div>
<p class="aa">aa1</p>
<p class="aa">aa2</p>
<p class="aa">aa3</p>
<p class="aa">aa4</p>
<p class="aa">aa5</p>
</div>

這樣每次的index依次為0,1,2,3,4;
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text(function(i,origTest){
return "Old text:"+origTest+"New text:+Hello(index)"+i;
})
})
$("#btn2").click(function(){
$("#test2").html(function(i,origTest){
return "Old html:"+origTest+"New html:+Hello(index)"+i;
})
})
});
</script>
</head>
<body>
<p id="test1">這是<b>粗體</b>文本。</p>
<p id="test2">這是另一段<b>粗體</b>文本。</p>
<button id="btn1">顯示舊/新文本</button>
<button id="btn2">顯示舊/新 HTML</button>
</body>
設定屬性 - attr()
jQuery attr() 方法也用于設定/改變屬性值。
下面的例子示範如何改變(設定)連結中 href 屬性的值:
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr("href","http://www.w3school.com.cn/jquery");
});
});
</script>
</head>
<body>
<p><a href="http://www.w3school.com.cn" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" id="w3s">W3School.com.cn</a></p>
<button>改變 href 值</button>
<p>請把滑鼠指針移動到連結上,或者點選該連結,來檢視已經改變的 href 值。</p>
</body>
attr() 方法也允許您同時設定多個屬性。
下面的例子示範如何同時設定 href 和 title 屬性:
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr({
"href" : "http://www.w3school.com.cn/jquery/",
"title" : "W3School jQuery 教程"
});
});
});
</script>
</head>
<body>
<p><a href="http://www.w3school.com.cn" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" id="w3s">W3School.com.cn</a></p>
<button>改變 href 和 title 值</button>
<p>請把滑鼠指針移動到連結上,或者點選該連結,來檢視已經改變的 href 值和已經設定的 title 值。</p>
</body>
attr() 的回調函數
jQuery 方法 attr(),也提供回調函數。回調函數由兩個參數:被選元素清單中目前元素的下标,以及原始(舊的)值。然後以函數新值傳回您希望使用的字元串。
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});
});
</script>
</head>
<body>
<p><a href="http://www.w3school.com.cn" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" id="w3s">w3school.com.cn</a></p>
<button>改變 href 值</button>
<p>請把滑鼠指針移動到連結上,或者點選該連結,來檢視已經改變的 href 值。</p>
</body>
添加元素:
可以很容易地添加新元素/内容。
添加新的 HTML 内容
我們将學習用于添加新内容的四個 jQuery 方法:
- append() - 在被選元素的結尾插入内容
- prepend() - 在被選元素的開頭插入内容
- after() - 在被選元素之後插入内容
- before() - 在被選元素之前插入内容
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").append(" <b>Appended text</b>.");
});
$("#btn2").click(function(){
$("ol").append("<li>Appended item</li>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>
<button id="btn1">追加文本</button>
<button id="btn2">追加清單項</button>
</body>
jQuery prepend() 方法在被選元素的開頭插入内容。
通過 append() 和 prepend() 方法添加若幹新元素
在上面的例子中,我們隻在被選元素的開頭/結尾插入文本/HTML。
<script>
function appendText()
{
var txt1="<p>Text.</p>"; // 以 HTML 建立新元素
var txt2=$("<p></p>").text("Text."); // 以 jQuery 建立新元素
var txt3=document.createElement("p");
txt3.innerHTML="Text."; // 通過 DOM 來建立文本
$("body").append(txt1,txt2,txt3); // 追加新元素
}
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button onclick="appendText()">追加文本</button>
</body>
after() 和 before() 方法
jQuery after() 方法在被選元素之後插入内容。
jQuery before() 方法在被選元素之前插入内容。
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("img").before("<b>Before</b>");
});
$("#btn2").click(function(){
$("img").after("<i>After</i>");
});
});
</script>
</head>
<body>
<img src="/i/eg_w3school.gif" alt="W3School Logo" />
<br><br>
<button id="btn1">在圖檔前面添加文本</button>
<button id="btn2">在圖檔後面添加文本</button>
</body>
添加多個:
<script>
function afterText()
{
var txt1="<b>I </b>"; // 以 HTML 建立元素
var txt2=$("<i></i>").text("love "); // 通過 jQuery 建立元素
var txt3=document.createElement("big"); // 通過 DOM 建立元素
txt3.innerHTML="jQuery!";
$("img").after(txt1,txt2,txt3); // 在 img 之後插入新元素
}
</script>
</head>
<body>
<img src="/i/eg_w3school.gif" alt="W3School Logo" />
<br><br>
<button onclick="afterText()">在圖檔後面添加文本</button>
</body>
删除元素:
删除元素/内容
如需删除元素和内容,一般可使用以下兩個 jQuery 方法:
- remove() - 删除被選元素(及其子元素)
- empty() - 從被選元素中删除子元素
$("#div1").remove();
div消失。
$(document).ready(function(){
$("button").click(function(){
$("#div1").empty();
});
});
</script>
删除div裡的内容。
過濾被删除的元素
jQuery remove() 方法也可接受一個參數,允許您對被删元素進行過濾。
該參數可以是任何 jQuery 選擇器的文法。
下面的例子删除 class="italic" 的所有 <p> 元素:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").remove(".italic");
})
});
</script>
</head>
<body>
<p>This is a paragraph in the div.</p>
<p class="italic"><i>This is another paragraph in the div.</i></p>
<p class="italic"><i>This is another paragraph in the div.</i></p>
<button>删除 class="italic" 的所有 p 元素</button>
</body>
擷取并設定 CSS 類
可以很容易地對 CSS 元素進行操作。
- addClass() - 向被選元素添加一個或多個類
- removeClass() - 從被選元素删除一個或多個類
- toggleClass() - 對被選元素進行添加/删除類的切換操作
- css() - 設定或傳回樣式屬性
可以給多個元素添加一個類也可以給一個類添加多個樣式
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
});
</script>
<style type="text/css">
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
</style>
$("button").click(function(){
$("#div1").addClass("important blue");
});
removeClass() 方法:
$("button").click(function(){
$("h1,h2,p").removeClass("blue");
});
toggleClass() 方法:
$("button").click(function(){
$("h1,h2,p").toggleClass("blue");
});
css() 方法:
css() 方法設定或傳回被選元素的一個或多個樣式屬性。
傳回 CSS 屬性:
css("propertyname");
設定 CSS 屬性:
css("propertyname","value");
設定多個 CSS 屬性:
$("p").css({"background-color":"yellow","font-size":"200%"});
尺寸:
很容易處理元素和浏覽器視窗的尺寸。
- width()
- height()
- innerWidth()
- innerHeight()
- outerWidth()
- outerHeight()
width() 方法設定或傳回元素的寬度(不包括内邊距、邊框或外邊距)。
height() 方法設定或傳回元素的高度(不包括内邊距、邊框或外邊距)。
<script>
$(document).ready(function(){
$("button").click(function(){
var txt="";
txt+="Width of div: " + $("#div1").width() + "</br>";
txt+="Height of div: " + $("#div1").height();
$("#div1").html(txt);
});
});
</script>
<div id="div1" style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div>
<br>
<button>顯示 div 的尺寸</button>
<p>width() - 傳回元素的寬度。</p>
<p>height() - 傳回元素的高度。</p>
innerWidth() 和 innerHeight() 方法
innerWidth() 方法傳回元素的寬度(包括内邊距)。
innerHeight() 方法傳回元素的高度(包括内邊距)。切記:有兩個,總和為内邊距*2+width/height.
outerWidth() 和 outerHeight() 方法
outerWidth() 方法傳回元素的寬度(包括内邊距和邊框)。
outerHeight() 方法傳回元素的高度(包括内邊距和邊框)。切記:内邊距*2+width/height+border*2.
outerWidth(true) 方法傳回元素的寬度(包括内邊距、邊框和外邊距)。
outerHeight(true) 方法傳回元素的高度(包括内邊距、邊框和外邊距)。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
var txt="";
txt+= "width"+$("#div1").width()+"<br />"
+"height"+$("#div1").height()+"<br />"
+"innerWidth"+$("#div1").innerWidth()+"<br />"
+"innerHeight"+$("#div1").innerHeight()+"<br />"
+"outerWidth"+$("#div1").outerWidth()+"<br/>"
+"outerHeight"+$("#div1").outerHeight()+"<br/>"
+"outerWidth"+$("#div1").outerWidth(true)+"<br/>"
+"outerHeight"+$("#div1").outerHeight(true)+"<br/>"
$("#div1").html(txt);
});
});
</script>
</head>
<body>
<div id="div1" style="height:300px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div>
<br>
<button>顯示 div 的尺寸</button>
<p>outerWidth(true) - 傳回元素的寬度(包括内邊距、邊框和外邊距)。</p>
<p>outerHeight(true) - 傳回元素的高度(包括内邊距、邊框和外邊距)。</p>
</body>
顯示文檔和視窗的尺寸:
$(document).ready(function(){
$("button").click(function(){
var txt="";
txt+="Document width/height: " + $(document).width();
txt+="x" + $(document).height() + "\n";
txt+="Window width/height: " + $(window).width();
txt+="x" + $(window).height();
alert(txt);
});
});
設定寬高:
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").width(320).height(320);
});
});
</script>
</head>
<body>
<div id="div1" style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div>
<br>
<button>調整 div 的尺寸</button>