jQuery基础
来源:互联网 发布:有没有在美迪学淘宝的 编辑:程序博客网 时间:2024/06/08 06:44
获得内容 - text()、html() 以及 val()
三个简单实用的用于 DOM 操作的 jQuery 方法:
- text() - 设置或返回所选元素的文本内容
- html() - 设置或返回所选元素的内容(包括 HTML 标记)
- val() - 设置或返回表单字段的值
获得内容 - text()、html() 以及 val()
三个简单实用的用于 DOM 操作的 jQuery 方法:
下面的例子演示如何通过 jQuery text() 和 html() 方法来获得内容:
实例
$("#btn1").click(function(){ alert("Text: " + $("#test").text());});$("#btn2").click(function(){ alert("HTML: " + $("#test").html());});
选择所有禁用的 <input> 和 <button> 元素:
$(":disabled")
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript"> $(document).ready(function(){ $(":disabled").css("background-color","#B2E0FF");}); </script></head><body><html><body><form action="">Name: <input type="text" name="user" /><br />Password: <input type="password" name="password" disabled="disabled" /><br /><button type="button">Useless Button</button><input type="button" value="Another useless button" disabled="disabled" /><br /><input type="reset" value="Reset" /><input type="submit" value="Submit" disabled="disabled" /><br /></form> </body></html> </body></html>
1.隐藏class=“”的html元素
<script>
$(document).ready(function(){
$("button").click(function(){
$(".main").hide();
});
});
</script>
<div class="main">123</div>
<div class="main">456</div>
<button type="button">点击隐藏</button>
2.隐藏所有标签为<p>的元素
<script>
$(document).ready(function(){
$("button").click(function(){
$('p').hide();
});
});
</script>
<button type="button">点击隐藏</button>
<p>隐藏1</p>
<p>隐藏2</p>
<p>隐藏3</p>
3.隐藏id=""的元素
<script>
$(document).ready(function(){
$("button").click(function(){
$("#main1").hide();
$("#main2").hide();
});
});
</script>
<div id="main1">123</div>
<div id="main2">456</div>
<button type="button">点击隐藏</button>
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// $("#n1").bind("click",function(){
$("#n1").click(function(){
if(document.getElementById("n1").checked){
document.getElementById("n2").disabled=true;
}else{
document.getElementById("n2").disabled=false;
}
});
// $("#n2").bind("click",function(){
$("#n2").click(function(){
if(document.getElementById("n2").checked){
document.getElementById("n1").disabled=true;
}else{
document.getElementById("n1").disabled=false;
}
});
});
</script>
</head>
<body>
<input type="checkbox" value="" id="n1" name="n1">第一个复选框
<input type="checkbox" value="" id="n2" name="n2">第二个复选框
</body>
</html>
<html>
<head>
<style>
#main{width:200px;height:100px;border:2px solid red;display:none;}
</style>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//bind() 方法为被选元素添加一个或多个事件处理程序,并规定事件发生时运行的函数
$("button").bind("click",function(){
$("div").slideToggle();
});
});
</script>
</head>
<body>
<div id="main">哈哈,被骗了吧</div>
<button>显灵吧</button>
</body>
</html>
替代方案
如果您不希望下载并存放 jQuery,那么也可以通过 CDN(内容分发网络) 引用它。
谷歌和微软的服务器都存有 jQuery 。
如需从谷歌或微软引用 jQuery,请使用以下代码之一:
Google CDN:
<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script></head>
Microsoft CDN:
<head><script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script></head>————————————————————————————————————————————————————————————————————————
jQuery 语法
jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作。
基础语法是:$(selector).action()
- 美元符号定义 jQuery
- 选择符(selector)“查询”和“查找” HTML 元素
- jQuery 的 action() 执行对元素的操作
示例
$(this).hide() - 隐藏当前元素
$("p").hide() - 隐藏所有段落
$("p.test").hide() - 隐藏所有 class="test" 的段落
$("#test").hide() - 隐藏所有 id="test" 的元素
jQuery 元素选择器
jQuery 使用 CSS 选择器来选取 HTML 元素。
$("p") 选取 <p> 元素。
$("p.intro") 选取所有 class="intro" 的 <p> 元素。
$("p#demo") 选取 id="demo" 的第一个 <p> 元素。
jQuery 属性选择器
jQuery 使用 XPath 表达式来选择带有给定属性的元素。
$("[href]") 选取所有带有 href 属性的元素。
$("[href='#']") 选取所有带有 href 值等于 "#" 的元素。
$("[href!='#']") 选取所有带有 href 值不等于 "#" 的元素。
$("[href$='.jpg']") 选取所有 href 值以 ".jpg" 结尾的元素。
jQuery CSS 选择器
jQuery CSS 选择器可用于改变 HTML 元素的 CSS 属性。
$(this)当前 HTML 元素$("p")所有 <p> 元素$("p.intro")所有 class="intro" 的 <p> 元素$(".intro")所有 class="intro" 的元素$("#intro")id="intro" 的第一个元素$("ul li:first")每个 <ul> 的第一个 <li> 元素$("[href$='.jpg']")所有带有以 ".jpg" 结尾的属性值的 href 属性$("div#intro .head")id="intro" 的 <div> 元素中的所有 class="head" 的元素jquery事件:
jQuery 事件方法
事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件。
触发实例:
$("button#demo").click()
上面的例子将触发 id="demo" 的 button 元素的 click 事件。
绑定实例:
$("button#demo").click(function(){$("img").hide()})
上面的例子会在点击 id="demo" 的按钮时隐藏所有图像。
each()函数:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("li").each(function(){
alert($(this).text())
});
});
});
</script>
</head>
<body>
<button>输出每个列表项的值</button>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
</html>
each()方法能使DOM循环结构简洁,不容易出错。each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组、多维数组、DOM, JSON 等等
$.each(arr1, function(i,val){
alert(i);
alert(val);
});
alert(val)将输出aaa,bbb,ccc
$.each(arr, function(i, item){
alert(i);
alert(item);
});
item[0]相对于取每一个一维数组里的第一个值
alert(i)将输出为0,1,2,因为这二维数组含有3个数组元素
alert(item)将输出为 ['a', 'aa', 'aaa'],['b', 'bb', 'bbb'],['c', 'cc', 'ccc']
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
//addClass()方法向被选元素添加一个或多个类
$(document).ready(function(){
$("button").click(function(){
$('p').addClass(function(n){
return 'par_' + n;
});
});
});
</script>
<style type="text/css">
.par_0
{
color:blue;
}
.par_1
{
color:red;
}
.par_2
{
color:#ddbbaa;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p>This is another paragraph too</p>
<button>向 p 元素添加类</button>
</body>
</html>
index() 方法返回指定元素相对于其他指定元素的 index 位置。
这些元素可通过 jQuery 选择器或 DOM 元素来指定。
注释:如果未找到元素,index() 将返回 -1。
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("li").click(function(){
alert($("li").index());
});
});
</script>
</head>
<body>
<p>点击列表项可获得其相对于同胞元素的 index 位置:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
</html>
removeClass() 方法从被选元素移除一个或多个类。
<html>
<head>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript">
//removeClass() 方法从被选元素移除一个或多个类
$(document).ready(function(){
$("button").click(function(){
$('ul li').removeClass(function() {
return 'listitem_' + $(this).index();
});
});
});
</script>
<style type="text/css">
.listitem_1
{
color:red;
}
.listitem_0
{
color:blue;
}
</style>
</head>
<body>
<h1 id="h1">This is a heading</h1>
<ul>
<li class="listitem_0">Apple</li>
<li class="listitem_1">IBM</li>
</ul>
<button>删除列表项中的类</button>
</body>
</html>
- JQuery基础
- jquery基础
- jquery基础
- jQuery基础
- jquery基础
- jquery基础
- jquery 基础
- jquery基础
- jQuery基础
- jquery 基础
- Jquery 基础
- jquery基础
- jquery基础
- jQuery基础
- jquery基础
- jquery基础
- jquery 基础
- jquery基础
- 优化:无约束共轭梯度法程序(c++)
- Java中获取键盘输入值的三种方法
- Mvc下使用js在本地组合xml、xslt文件
- CPU频率说明
- C++ 中函数后面跟const是什么意思
- jQuery基础
- javascript 日期 获取周等
- 用js返回上一页并刷新
- myeclipse四大优化议案 myeclipse 10
- 第12次课堂作业
- sqlserver2005 单个用户更改为多用户
- razor
- 高手分享:快速处理IE如何打开网页的问题——认证系统
- 关于hibernate中实体中equals和hashcode方法的重写