jQuery 入门
不能正常引用jQuery-2.2.4.min.js所以代码没生效
jQuery
是一个 JavaScript 函数库。jQuery 库包含以下特性:
HTML 元素选取
HTML 元素操作
CSS 操作
HTML 事件函数
JavaScript 特效和动画
HTML DOM 遍历和修改
AJAX
Utilities
书写规范
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
//代码不能写在引用文件这个容器里
<script>
$somecode //$符号就表示jQuery
jQuery.somecode //等同
</script>
PS:由于编辑器的JS编码问题中文在js编码过程中没有使用utf-8.显示乱码.实际代码和注释源码有区别
选择器和筛选器
类似JS有#id(id选择器),element(元素选择器即标签),class(class选择器),组合选择器(selector多种选择器组合),层级选择器,*(选择全部)...
基本选择器
在js基础上稍微修改了一下,是用#代表id, .class代表class,标签直接使用标签名
PS:id选择器,使用任何的元字符(如 !"#$%&'()*+,./:;<=>?@[]^`{|}~)作为名称的文本部分, 它必须被两个反斜杠转义:\。 参见示例。
<div id="testid"></div>
<div class="testclass"></div>
<p></p>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous" charset="utf-8"></script>
<script>
$('#testid').text('#testid相当于js: docment.getElementById("testid")')
$('p').text('p相当于js: document.getElementsByTagName("p")')
$('.testclass').text('.testclass相当于js: document.getElementByClassName("testclass")')
组合选择器
类似js定义
<div id="testid1"></div>
<div class="testclass1"></div>
<script>
$('#testid1, .testclass1').text('逗号隔开元素组合选择器')
</script>
层级选择器
和JS一样通过空格隔开选择器表达层级路径
<form>
<p>原始</p>
<div id="test">
<p>原始</p>
</div>
</form>
<p >原始</p>
<script>
$('form #test p').text('层级选中')
</script>
原始
原始
筛选器
first选择匹配到的第一个
<li>list item 1</li>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
<script>
$('ul li').first().text('first')
</script>
list item 1
- list item 1
- list item 2
- list item 3
- list item 4
- list item 5
属性选择器
attr设置或返回被选元素的属性值。
<div id="testattr">testattr</div>
<script>
$('#testattr').attr('name','test')
</script>
removeAttr从每一个匹配的元素中删除一个属性
<div id="rmattr" name="test">rmattr</div>
<script>
$('#rmattr').removeAttr('name')
</script>
html和text
js中的innerHtml和innerText
<p id="testtext">testtext</p>
<p id="testhtml">testhtml</p>
<script>
$('#testtext').text('changed')
$('#testhtml').html('test<br>html')
</script>
testtext
testhtml
val
<input type="text" id="testval">testval</input>
<script>
//$('input:text').val('hello')
$('#testval').val('world')
</script>
testval
CSS
<div id="testcss">testcss</div>
<script>
//$('#testcss').height(20)
//$('#testcss').width(20)
$('#testcss').css
({"color":"white","background":"blue","height":"80px", "width":"80px"})
</script>
testcssjQuery还有很多选择器.慢慢看文档吧..很简单,把很多JS复杂的查找封装了很多易用方法,包括筛选器,属性也是类似的方法.
事件和文档处理
不是专业做前端的话,东西看起来还是有点多,慢慢看吧,都是这么玩的.
jQuery小例
- 菜单切换
<style>
.tab-box{
height: 300px;
width: 300px;
}
.tab-box a {
border-right: 2px;
padding: 8px;
}
.tab-box .box-menu{
line-height: 33px;
background-color: #dddddd;
border: 1px solid #dddddd;
}
.tab-box .box-body{
border: 1px solid;
background-color: white;
}
.hide{
display: none;
}
.current{
background-color: white;
color: black;
border-top:2px solid red;
}
</style>
<div class="tab-box">
<div class="box-menu">
<a menu-val="1" onclick="ChangeMenu(this);" class="current">菜单一</a>
<a menu-val="2" onclick="ChangeMenu(this);">菜单二</a>
<a menu-val="3" onclick="ChangeMenu(this);">菜单三</a>
</div>
<div class="box-body">
<div id="content1">内容一</div>
<div id="content2" class="hide">内容二</div>
<div id="content3" class="hide">内容三</div>
</div>
</div>
<script>
function ChangeMenu(ths) {
$(ths).addClass('current').siblings().removeClass('current');
//找到当前点击的标签,加上选中样式
var contentId = $(ths).attr('menu-val');
//获取当前标签的mene-val
var tmp = '#content' + contentId;
$(tmp).removeClass('hide').siblings().addClass('hide');
//将对应标签menu-val对应的内容标签移除hide属性,给其他没有选中的内容添加hide
//console.log($('.tab-box').html())
}
</script>
.tab-box{
height: 300px;
width: 300px;
}
.tab-box a {
border-right: 2px;
padding: 8px;
}
.tab-box .box-menu{
line-height: 33px;
background-color: #dddddd;
border: 1px solid #dddddd;
}
.tab-box .box-body{
border: 1px solid;
background-color: white;
}
.hide{
display: none;
}
.current{
background-color: white;
color: black;
border-top:2px solid red;
}
- 循环each方法使用,全选,反选,取消
<div>
<input type="button" value="全选" onclick="SelectAll();" />
<input type="button" value="反选" onclick="ReverseAll();" />
<input type="button" value="取消" onclick="ClearAll();" />
</div>
<div>
<table border="1">
<tr>
<td><input type="checkbox"></td>
<td>123</td>
<td>123</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>123</td>
<td>123</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>123</td>
<td>123</td>
</tr>
</table>
</div>
<script>
function SelectAll() {
//全选: 获得$('table :checkbox')全部将checked属性改为true
$('table :checkbox').prop('checked', true);
}
function ClearAll() {
//取消: 获得$('table :checkbox')全部将checked属性改为false
$('table :checkbox').prop('checked', false);
}
function ReverseAll() {
// $('table :checkbox')获得所有table :checkbox列表
//jQuery封装了for循环each(callback),这里会把每个元素传入each(里的function(){}执行)
$('table :checkbox').each(function () {
var isChecked = $(this).prop('checked');
//$this在循环内获得当前元素
if(isChecked){
$(this).prop('checked', false);
}else {
$(this).prop('checked', true);
}
})
//JS原本写法
// var checkboxList = $('table :checkbox');
// for(var i in checkboxList){
// var ele = checkboxList[i];
// var isChecked = $(ele).prop('checked');
// if(isChecked){
// $(ele).prop('checked', false)
// }else {
// $(ele).prop('checked', true)
// }
// }
}
</script>
<input type="button" value="全选" onclick="SelectAll();" />
<input type="button" value="反选" onclick="ReverseAll();" />
<input type="button" value="取消" onclick="ClearAll();" />
123 123 123 123 123 123 PS: each也可以处理字典,each()还有一个书写方式,
var testList = [11,22,33,44];
$.each(userList, function(i,item){ //i,item分别对应userList列表的index和值
some code;
})返回顶部
<style>
.go-top{
background-color: blue;
color: white;
position: fixed;
right: 0;
bottom:0;
width: 60px;
border: 2px solid #2728ff;
line-height: 10px;
text-align: center;
cursor: pointer;
}
.hide{
display: none;
}
</style>
<div class="go-top hide">
<a onclick="GoTop();">返回顶部</a>
</div>
<script>
window.onscroll = function () {
var currentTop = $(window).scrollTop()
if(currentTop > 100){
$('.go-top').removeClass('hide')
}else {
$('.go-top').addClass('hide')
}
}
function GoTop() {
$(window).scrollTop(0)
}
</script>
.go-top{
background-color: blue;
color: white;
position: fixed;
right: 0;
bottom:0;
width: 60px;
border: 2px solid #2728ff;
line-height: 10px;
text-align: center;
cursor: pointer;
}
.hide{
display: none;
}
可移动pannal
<div style="border: 1px solid #ddd;width: 600px;position: absolute;">
<div id="title" style="background-color: black;height: 40px;color: white;">
标题
</div>
<div style="height: 300px;">
内容
</div>
</div>
<script>
$(function(){
// 页面加载完成之后自动执行$(function(){}) $(document).ready(function(){})
$('#title').mouseover(function(){
$(this).css('cursor','move');
}).mousedown(function(e){
var _event = e || window.event;
//有些浏览器支持e,有些浏览器不支持e,就使用window.eventhuoqu鼠标位置
var ord_x = _event.clientX;
var ord_y = _event.clientY;
//获取当前鼠标,
var parent_left = $(this).parent().offset().left;
var parent_top = $(this).parent().offset().top;
//this为当前标签,获取标签的位置
$(this).bind('mousemove', function(e){
//给鼠标绑定事件
var _new_event = e || window.event;
var new_x = _new_event.clientX;
var new_y = _new_event.clientY;
//获取新的位置
var x = parent_left + (new_x - ord_x);
var y = parent_top + (new_y - ord_y);
//获取偏移量
$(this).parent().css('left',x+'px');
$(this).parent().css('top',y+'px');
//将当前标签位置加上偏移量
})
}).mouseup(function(){
$(this).unbind('mousemove');
});
})
bootstrap
说了这么多,其实就是因为一直看不懂,玩不来bootstrap.所以从头学习了一下html到jquery.
中文网站: 点击
jQuery 入门的更多相关文章
- jQuery入门(1)jQuery中万能的选择器
jQuery入门(1)jQuery中万能的选择器 jQuery入门(2)使用jQuery操作元素的属性与样式 jQuery入门(3)事件与事件对象 jQuery入门(4)jQuery中的Ajax()应 ...
- jQuery入门(2)使用jQuery操作元素的属性与样式
jQuery入门(1)jQuery中万能的选择器 jQuery入门(2)使用jQuery操作元素的属性与样式 jQuery入门(3)事件与事件对象 jQuery入门(4)jQuery中的Ajax()应 ...
- jQuery入门(3)事件与事件对象
jQuery入门(1)jQuery中万能的选择器 jQuery入门(2)使用jQuery操作元素的属性与样式 jQuery入门(3)事件与事件对象 jQuery入门(4)jQuery中的Ajax()应 ...
- jQuery入门(4)jQuery中的Ajax应用
jQuery入门(1)jQuery中万能的选择器 jQuery入门(2)使用jQuery操作元素的属性与样式 jQuery入门(3)事件与事件对象 jQuery入门(4)jQuery中的Ajax()应 ...
- JQuery入门
JQuery入门 1 jQuery的概述 1.1 jQuery简介 jQuery是一个 JavaScript函数库,它是一个“写的更少,但做的更多”的轻量级 JavaScript 库.jQuery 极 ...
- jQuery入门必须掌握的一些API
jQuery 中文版文档:http://www.css88.com/jqapi-1.9/category/ajax/ jQuery入门,必须掌握以下的API,平时工作中经常会用到.未列出的API,在掌 ...
- Web前端JQuery入门实战案例
前端jquery入门到实战 为什么要学习Jquery?因为生活. 案例: <!DOCTYPE html> <html lang="zh-CN"> <h ...
- jquery(入门篇)无缝滚动
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 第一百六十二节,jQuery入门介绍
jQuery入门 学习要点: 1.什么是 jQuery 2.学习 jQuery的条件 3.jQuery的版本 4.jQuery的功能和优势 5.其他 JavaScript库 6.是否兼容低版本 I ...
- jQuery入门--- 非常好
jQuery入门------https://blog.csdn.net/dkh_321/article/details/78093788
随机推荐
- C++ vector 常用API
vector: 向量容器,动态数组,类模板 定义和初始化: vector<T> v1; //v1是空vector,元素类型是T类型,执行默认初始化,int为0,string为空串 vect ...
- Python笔记·第六章——字典 (dict) 的增删改查及其他方法
字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可 ...
- 多线程编程学习笔记——async和await(二)
接上文 多线程编程学习笔记——async和await(一) 三. 对连续的异步任务使用await操作符 本示例学习如何阅读有多个await方法方法时,程序的实际流程是怎么样的,理解await的异步 ...
- vue项目的骨架及常用组件介绍
vue项目基础结构 一个vue的项目,我觉得最小的子集其实就是{vue,vue-router,component},vue作为基础库,为我们提供双向绑定等功能.vue-router连接不同的" ...
- 网络相关系列之三:通过GET和POST方法发送数据
写在最前面:年少的安逸舒适在随着年龄的到来和现实生活的压迫总有一天会全数归还(事实上就是<无间道>中那句:"出来混,迟早要还的!") so fighting. 一.GE ...
- Ubuntu Linux訪问小米手机存储卡
操作系统: 麒麟14.04 安装工具 sudo apt-get install mtpfs libfuse-dev libmad0-dev sudo mkdir /media/mtp 重新启动与使用 ...
- android 程序执行linux命令注意事项
一:问题描述 在已经root过的android设备下,app执行一个linux命令,app需要获取su权限,在某些android主板下会出现异常, Command: [su] Working D ...
- Effective Java 第三版——7. 消除过期的对象引用
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- IntelliJ Idea设置护眼浅绿色背景方法
老版本的设置位置 新版本的设置位置不一样(下图新版本为2017.5.4)的设置位置
- Intellijidea建javaWeb以及Servlet简单实现
一.创建并设置javaweb工程1.创建javaweb工程File --> New --> Project... 点击Project后出现如下界面,选择Java Enterprise,选中 ...