前端学习之jquery

一 属性操作

html():
console.log($("div").html());
$(".test").html("<h1>hello</h1>"); 拿到的是标签 test():
console.log($("div").text());
$(".test").text("<h1>hello</h1>"); 整个纯文本
val():val针对的是固有属性value
console.log($(":text").val());
console.log($(":checkbox").val());
console.log($(":button").val())//val针对的是固有属性value

tab切换案例:

<style>
*{
margin: 0;
padding: 0;
} .outer{
width: 80%;
margin: 0 auto; } .nav li{
float: left;
list-style: none;
width: 100px;
height: 40px;
background-color: #6AA1EA;
text-align: center;
line-height: 40px;
border-right: 2px solid green;
} .content{
width: 306px;
height: 400px;
background-color: #99cc99;
margin-top: 40px; } ul .active{
background-color: #99aecb;
} .hide{
display: none;
}
</style> </head>
<body> <div class="outer">
<ul class="nav">
<li f="con1" class="active">菜单一</li>
<li f="con2">菜单二</li>
<li f="con3">菜单三</li>
</ul> <div class="content">
<div class="con1">111</div>
<div class="con2 hide">222</div>
<div class="con3 hide">333</div>
</div> </div> <script src="jquery-3.2.1.js"></script>
<script>
var outer=document.getElementsByClassName("outer")[0];
var lis=outer.getElementsByTagName("li");
for(var i=0;i<lis.length;i++){
lis[i].onclick=function () { $(this).addClass("active").siblings().removeClass("active"); var $name=$(this).attr("f"); $("."+$name).removeClass("hide").siblings().addClass("hide")
} } </script>

tab切换案例

二 jquery循环方式

方法一:
//$.each(arr,funtion(){}) //循环语法
arr=[123,456,"hello"];
obj={"name":"egon","age":22};
$.each(arr,function (i,j) {
console.log(i,j)
})
$.each(obj,function (i,j) {
console.log(i,j)
}) 方法二:
$().each(function () {
})
<ul>
<li>111</li>
<li class="item">222</li>
<li>333</li>
</ul>
$("ul li").each(function () {
console.log($(this))
if ($(this).hasClass("item")){
alert($(this).text())
}
})

table正反选示例:

<body>

<h1>表格示例</h1>
<button>全选</button>
<button>反选</button>
<button>取消</button>
<hr> <table border="2px">
<tr>
<td><input type="checkbox" class="item"></td>
<td>111</td>
<td>111</td>
<td>111</td>
</tr>
<tr>
<td><input type="checkbox" class="item"></td>
<td>111</td>
<td>111</td>
<td>111</td>
</tr>
<tr>
<td><input type="checkbox" class="item"></td>
<td>111</td>
<td>111</td>
<td>111</td>
</tr>
<tr>
<td><input type="checkbox" class="item"></td>
<td>111</td>
<td>111</td>
<td>111</td>
</tr> </table> <script src="jquery-3.2.1.js"></script>
<script>
var eles=document.getElementsByTagName("button");
// var inps=document.getElementsByClassName("item");
for(var i=0;i<eles.length;i++){
eles[i].onclick=function () {
if (this.innerText=="全选"){
// for(var j=0;j<inps.length;j++){
// inps[j].checked=true
// }
$(":checkbox").prop("checked",true);
}
else if (this.innerText=="取消"){
$(":checkbox").prop("checked",false);
}
else{
$(":checkbox").each(function () {
$(this).prop("checked",!$(this).prop("checked")); //最新优化
// if ($(this).prop("checked")){
// $(this).prop("checked",false)
// }
// else {
// $(this).prop("checked",true)
// }
})
}
}
} </script> </body>

table 正反选示例

三 动画效果

hide()与show()隐藏与显示

<p>hello</p>
<img src="top.jpg" width="400px" height="200px">
<button id="hide">隐藏</button>
<button id="=show">显示</button>
<button id="toggle">切换</button> <script src="jquery-3.2.1.js"></script> <script> //用于切换备选元素的 hide()与show()方法。
// 标签对象.事件(function(){})
$("#hide").click(function () {
$("img").hide(1000); });
$("#show").click(function () {
$("img").show(1000);
}); $("#toggle").click(function () {
$("img").toggle(1000);
}) </script>

滑动效果:

slideDown()  slideUp()  slideToggle()

<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#con{
line-height: 80px;
background-color: #015e98;
} </style>
</head>
<body> <div id="con">滑动效果</div> <button id="slideDown">slideDown</button>
<button id="slideUp">slideUp</button>
<button id="toggle">toggle</button> <script src="jquery-3.2.1.js"></script> <script>
$("#slideDown").click(function () {
$("#con").slideDown(1000)
}); $("#slideUp").click(function () {
$("#con").slideUp(1000)
}); $("#toggle").click(function () {
$("#con").slideToggle(1000)
})
</script> </script>
</body>

淡入淡出透明度:

fadeIn()  fadeout()

<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#con{
height: 200px;
width: 200px;
background-color:#ccecef;
}
</style> </head>
<body> <div id="con"></div> <button id="fadeIn">fadeIn</button>
<button id="fadeOut">fadeOut</button>
<button id="fadeToggle">fadeToggle</button>
<button id="fadeTo">fadeTo</button> <script src="jquery-3.2.1.js"></script>
<script> $("#fadeIn").click(function () {
$("#con").fadeIn(2000)
});
$("#fadeOut").click(function () {
$("#con").fadeOut(2000)
});
$("#fadeToggle").click(function () {
$("#con").fadeToggle(2000)
})
$("#fadeTo").click(function () {
$("#con").fadeTo(1000,0.4) //透明度
}) </script>

回调函数:当某一个动作执行完成之后自动触发的函数

四 文档操作

内部插入:

(append)  ( prepend) //父节点添加子节点

<script>
$("button").click(function () {
$("div").append("<h1>hello</h1>");//插入的位置不一样 一个在后面
$("div").prepend("<h1>hi</h1>"); //一个在前面插入
})
</script>

(appendTo)(perpendTo) //子节点插入父节点

<script>
$("button").click(function () {
var $ele=$("<p>hello</p>");
$ele.appendTo("div") //子节点插入父节点
$ele.prependTo("div")//
});
</script>

外部插入:兄弟之间插入

after() before()

$("div").after("<p>pppppp</p>");
$("div").before("<p>pppppp</p>")

insertAfter()   insertBefore()

var $ele=$("<p>hello</p>");
// $ele.insertAfter("div")
$ele.insertBefore("div")

替换replaceWith()

$("div").replaceWith("<h1>egon</h1>")  

删除

//$("div").empty()  //清空文本内容
$("div").remove() //清空整个标签

复制(克隆)

<div class="box">
<div class="item">
<input type="button" value="+">
<input type="text">
</div>
</div> <script src="jquery-3.2.1.js"></script>
<script>
$(":button").click(function () {
//var $clone=$(".box .item").clone(); //1变2 2变4
var $clone=$(this).parent().clone();
$clone.children(":button").val("-").attr("onclick","removeA(this)");
$clone.children();
$(".box").append($clone)
});
function removeA(self) {
console.log($(self).parent());
$(self).parent().remove()
}
</script>

前端学习之jquery/下的更多相关文章

  1. 前端学习之jquery

    前端学习之jquery 1.   什么是jQuery对象? jQuery对象就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的.如果一个对象是jQuery对象,那么它 ...

  2. 第四篇 前端学习之JQuery基础

    一 jQuery是什么? jQuery就是一个JavaScript的库. <1> jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入 ...

  3. 前端学习之-- Jquery

    Jquery学习笔记 中文参考文档:http://jquery.cuishifeng.cn Jquery是一个包含DOM/BOM/JavaScript的类库引入jquery文件方法:<scrip ...

  4. 前端学习之jquery(二)

    操作元素(属性,css,文档处理) 1.1 属性操作 --------------------------属性 $("").attr(); $("").remo ...

  5. web前端学习总结--JQuery

    jQuery 什么是jQuery jQuery是一个优秀的JavaScript框架,一个轻量级的JS库. 它封装了JS.CSS.DOM,提供了一致的.简洁的API. 兼容CSS3,及各种浏览器 使用户 ...

  6. 前端学习之三——jquery选择器

    Jquery中的选择器分为几大类:基本过滤选择器,层次选择器,内容过滤选择器,可见性过滤选择器,属性过滤选择器,子元素过滤选择器,表单对象选择器和表单对象属相过滤选择器. 1.非基本过滤选择器,一般需 ...

  7. web前端开发学习:jQuery的原型中的init

    web前端开发学习:jQuery的原型中的init 有大量web前端开发工具及学习资料,可以搜群[ web前端学习部落22群 ]进行下载,遇到学习问题也可以问群内专家以及课程老师哟 jQuery.fn ...

  8. jQuery延迟加载(懒加载)插件 – jquery.lazyload.js-Web前端(W3Cways.com) - Web前端学习之路

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  9. 《疯狂前端开发讲义jQuery+Angular+Bootstrap前端开发实践》学习笔记

    <疯狂前端开发讲义jQuery+Angular+Bootstrap前端开发实践>学习笔记 二〇一九年二月十三日星期三2时28分54秒 前提:本书适合有初步HTML.CSS.JavaScri ...

随机推荐

  1. Filecoin:募资详情和Token分发详情

    基本情况 总数:20亿枚 参与资格:美国合格投资人身份认证(采用与IPO相同的流程,以确保合法性) 爱西欧占比:10%(2亿枚) 爱西欧总金额:2.57亿美元 私募 时间:2017.7.21~2017 ...

  2. PHP7变量的内部实现

    PHP7变量的内部实现 受篇幅限制,这篇文章将分为两个部分.本部分会讲解PHP5和PHP7在zval结构体的差异,同时也会讨论引用的实现.第二部分会深入探究一些数据类型如string和对象的实现. P ...

  3. NGUI_Toggle

    七.Toggle复选框:就是对一个选项做上一个标记,表示这个选项已经被选中了. 1.当我们要判断是否使用复选框,可以遵循一下规律: (1).该功能只有两种状态  是/否 (2).该功能同一时间只能激活 ...

  4. webpack4新特性介绍

    导语: webpack是一个JS应用打包器, 它将应用中的各个模块打成一个或者多个bundle文件.借助loaders和plugins,它可以改变.压缩和优化各种各样的文件.它的输入是不同的资源,比如 ...

  5. 在RE了16次之后,没想到还可以这样Runtime error

    这是POJ: RE的原因: 比如: int b=2147483647; for(int i=0;i<=b;++i){ .... } 应该懂了吧, 2147483647是int能表示的最大整数 解 ...

  6. 最小化安装CentOS7的网卡设置

    实验环境:CentOS 7 Minimal Installation 64bit (1511) 最小化安装CentOS 7 后,查看网卡的信息让人很意外,因为网卡的命名规则变了,网卡的名字让人很难懂. ...

  7. 从 MVC 到前后端分离

    从 MVC 到前后端分离 1 理解 MVC MVC 是一种经典的设计模式,全名为 Model-View-Controller,即 模型-视图-控制器. 其中,模型 是用于封装数据的载体,例如,在 Ja ...

  8. Algorithm --> 最长回文子串

    1.中心扩展 中心扩展就是把给定的字符串的每一个字母当做中心,向两边扩展,这样来找最长的子回文串.算法复杂度为O(N^2). 但是要考虑两种情况: 1.像aba,这样长度为奇数. 2.想abba,这样 ...

  9. PHP Session 常用的函数

    我们在前面的文章里面讲到session的原理和最佳实践,感到意犹未尽.现在再来聊下PHP Session用到的几个相关的函数. session_start() session_start() 会创建新 ...

  10. Python3 的描述符--完整例子详细解释

    ##描述符类的例子,这个例子说明了描述符和被描述符之间的关系 ##摄氏温度 class Celsius(): ## 1 描述符类 def __init__(self,value = 26.0): ## ...