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
随机推荐
- 逐步搭建Lamp环境之Linux的运行模式
首先先来看几个概念,分别是:单用户.单任务.多用户.多任务 单用户: 是指操作系统一般只能由一个人同时进行登录 单任务: 是指操作系统只能同时处理一个任务 多用户: 是指操作系统可以允许由多个用户同时 ...
- 20150605面试汇总--js与java的差别
javascript与java都是编程语言,不同在于代码格式不同. js基于对象,java是面向对象: java是强变量.编译前必须作出声明.js是弱变量,使用前不需做声明: JavaScript 是 ...
- Java方法的概念及使用
方法 将一段逻辑或者功能提取出来,这种提取的形式就是函数 格式 修饰符 返回值类型 函数名(参数列表){ 方法体: return 返回值; } //明确返回值类型---求两个整数的和,确定结果一定是整 ...
- 3D Game Programming withDX11 学习笔记(一) 数学知识总结
在图形学中,数学是不可或缺的一部分,所以本书最开始的部分就是数学知识的复习.在图形学中,最常用的是矢量和矩阵,所以我根据前面三个章节的数学知识,总结一下数学知识. 一.矢量 数学中的矢量,拥有方向和长 ...
- Android查缺补漏--ContentProvider的使用
ContentProvider (内容提供者)是一种共享型组件,可以为系统内应用于与应用之间提供访问接口. ContentProvide要想正常工作需要三个关键点: ContentProvider:对 ...
- iOS 用户密码 数字字母特殊符号设置 判断
//直接调用这个方法就行 -(int)checkIsHaveNumAndLetter:(NSString*)password{ //数字条件 NSRegularExpression *tNumRegu ...
- Libevent源码分析 (1) hello-world
Libevent源码分析 (1) hello-world ⑨月份接触了久闻大名的libevent,当时想读读源码,可是由于事情比较多一直没有时间,现在手头的东西基本告一段落了,我准备读读libeven ...
- C++11新语法糖之尾置返回类型
C++11的尾置返回类型初衷是为了方便复杂函数的声明和定义,但是当复杂度稍微提升一些的时候很明显能注意到这种设计的作用微乎其微. 首先考虑如下代码: C++ //返回指向数组的指针 auto func ...
- Nginx的 HTTP 499 状态码处理
1.前言 今天在处理一个客户问题,遇到Nginx access log中出现大量的499状态码.实际场景是:客户的域名通过cname解析到我们的Nginx反向代理集群上来,客户的Web服务是由一个负载 ...
- bzoj 2337: [HNOI2011]XOR和路径
Description Input Output Sample Input Sample Output HINT Source Day2 终于把这个史前遗留的坑给填了... 首先异或的话由位无关性,可 ...