首页 技术 正文
技术 2022年11月15日
0 收藏 589 点赞 4,472 浏览 2739 个字

jquery基础知识

1.jquery文件的引入,所有的js代码要写在下面那段代码下面。

<script src="../jquery-1.11.2.min.js"></script><!--引入的jquery一定是在最上面的,也要在其它引入的jquery文件上面-->

2.写jquery代码的位置

和js一样,jquery代码也是写在<script>开始和结束标签之间。

<script type="text/javascript">
</script>

jquery选取元素

1.根据id找元素

先在<body>里面写一个<div>

<div id="a1"></div>

(1)用js找,取到的是具体的元素。

var a = document.getElementById("a1");
alert(a);

(2)用jquery找,取到的是jquery对象。

var b = $("#a1");//用jquery找元素的写法,#也是代表根据id找。
alert(b);
alert(b[0]);//从对象里面取元素

2.根据class找元素

先先在<body>里面写2个<div>

<div class="aa"></div>
<div class="aa"></div>

(1)用js找

var a = document.getElementsByClassName("aa");
alert(a);

(2)用jquery找

var b = $(".aa")
//alert(b);
//alert(b[0]);//取到第一个div
//alert(b[1]);//取到第二个div
alert(b.eq(0));//取jquery对象用eq(),取元素本身用[]。

3.根据标签名取元素

(1)用js找

var a = document.getElementsByTagName("div");

(2)用jquery找

var b = $("div");
alert(b);

4.根据name取

先先在<body>里面写1个<div>

<div name="cc"></div>

(1)用js找

var a = document.getElementsByName("cc");

(2)用jquery找

var b = $("[name=cc]");//根据属性筛选,只要是属性的,都可以找到。
//alert(b);
alert(b[0]);

jquery操作元素

1.操作内容

(1)非表单元素

<div id="a1">11</div>
var a = document.getElementById("a1");
//a.innerText = "hello";
a.innerHTML = "<span style='color:red'>world</span>";

(2)表单元素

<input type="text" id="p1" />
var a = document.getElementById("p1");
a.value="hello";

2.操作元素

(1)非表单元素

  赋值:

<div id="a1">11</div>
var b = $("#a1");
b.text("aaa")
//b.html("aaa")

(2)表单元素

赋值

var b = $("#a1");
b.val("aaa")

取值

var b = $("#a1");
b.val();

3.操作属性

js操作属性

a.setAttribute("","");
a.setAttribute("");
a.removeAttribute("");

jquery操作属性

var b = $("#a1");b.attr("bs","1");//添加
 b.attr("bs");//获取bs属性的值
 b.removeAttr("bs");//移除bs属性

4.操作样式

(1)js操作样式

  js不能获取内嵌的属性,只能获取内联的。

a.style.color = red;

(2)jquery操作样式

  jquery可以获取、设置内嵌的、外部的、内联的样式。

alert(b.css("width"));//获取样式
b.css("font-size","50px");//设置样式

隐藏3个div的做法

<style type="text/css">
.aa{ width:100px; height:100px; background-color:#0F0}<!--用内嵌的方式写属性-->
</style>
<div class="aa"></div>
<div class="aa"></div>
<div class="aa"></div>

(1)js的写法

var a = document.getElementsByClassName("aa");
for(var i=0;i<a.length;i++)
{
a[i].style.display = "none";
}

(2)jquery的写法

$(".aa").css("display","none");

jquery事件、挂事件、移除事件

<div id="a1">11</div>
<div class="aa" bs="1">aaa</div>
<div class="aa" bs="2">bbb</div>
<div class="aa" bs="3">ccc</div><input type="text" id="p1" />
<input type="button" id="b1" value="挂事件" />
<input type="button" id="b2" value="移除事件" />
//jquery加事件$(document).ready(function(e) {//页面加载完成之后执行事件        //给a1加点击
/*$("#a1").click(function(){
alert('aa');
}) //给class为aa的所有元素加事件
$(".aa").click(function(){
//alert('bb');
//alert($(this).text());//取文本值。this点击哪一个就选取哪一个
//alert($(this).attr("bs"));//取属性值 $(".aa").css("background-color","#0F0");//先让所有的颜色变为原来的颜色
$(this).css("background-color","red");//点击哪一个背景颜色换成红色
})*/ //第二种方式挂事件
$("#b1").click(function(){//匿名函数 $("#a1").bind("click",function(){//bind表示挂事件 alert("我是挂上的事件"); }) $("#b2").click(function(){ $("#a1").unbind("click");//unbind移除事件 }) })});
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289