jQuery小例子
map遍历数组
//=========for循环遍历==========
var arr[1,2,3,4,5];
for(var i=0;i<=arr.length;i++)
{
arr[i]=arr+1;
}
//对数组中的每个元素加1
var scores = [10, 20, 30, 40, 50];
var newScore = [];
for (var i = 0; i < scores.length; i++) {
newScore[newScore.length] = scores[i] + 1;
}
//=========map遍历============
$.map(arr,function(ele,index))
{//ele代表元素,index代表索引
alert(ele+'====='+index);
});
//对数组中索引大于3的元素翻倍
var arr = [1, 2, 4,2,3,4,5,6,5];
arr = $.map(arr, function (ele, index) {
return index > 3 ? ele * 2 : ele;
});
jQuery对象与DOM对象才做元素和互转
//dom 操作
var dvObj = document.getElementById('dv');
dvObj.style.border = '1px solid red';
dvObj.style.width = '300px';
dvObj.style.height = '200px';
//dom转jQuery
var dvj=$(dvObj);
dvj.css('backgroundColor', 'yellow').css('width', '500px').css('height', '300px').css('border', '1px solid blue');
//jquery转dom
var dvooo=dvj[0]//.get(0);
prevall与nextall
//页面上有一个ul球队列表当鼠标移动到某个li上的时候改行背景颜色变红,当点击某个li的时候,让该li之前的所有li背景色变黄,之后的所有li背景色变蓝。自己不变色。
$(function () { $('#u1 li').mouseover(function () { $(this).css('backgroundColor', 'red');
}).click(function () {
// $(this).prevAll().css('backgroundColor', 'yellow');
//$(this).nextAll().css('backgroundColor', 'blue');
//断链--end()修复断链
$(this).prevAll().css('backgroundColor', 'yellow').end().nextAll().css('backgroundColor', 'blue');
}).mouseout(function () {
$(this).css('backgroundColor', '');
}); });
jquery版的星星评分控件
<script type="text/javascript">
$(function () {
$('#tb td').mouseover(function () {
$(this).text('★').prevAll().text('★'); //.end().nextAll().text('☆');
}).mouseout(function () {
$('#tb td').text('☆');
});
});
</script>
jquery为元素添加样式,去除样式,切换样式
//为某元素添加一个类样式
// $('div').addClass('cls cls1');
//移除一个类样式
// $('div').removeClass('cls');
//判断这个元素是否应用了这个类样式
//$('div').hasClass('cls');
// $('body').toggleClass('cls');//切换该类样式
jquery索引选择器
//索引为2的那个变了
//$('p:eq(2)').css('backgroundColor','red');
//索引小于2的元素
// $('p:lt(2)').css('backgroundColor', 'red');
//索引大于2的
//$('p:gt(2)').css('backgroundColor', 'red');
siblings与even与odd
$(function () {
$('table tr').click(function () {
//除此之外,其他的所有td
$('td', $(this).siblings()).css('backgroundColor','');
//偶数行的td
$('td:even', $(this)).css('backgroundColor', 'red');
//奇数行的td
$('td:odd', $(this)).css('backgroundColor', 'blue');
});
});
属性过滤器
//获取层中所有有name属性的input标签元素
//$('#dv input[name]').css('backgroundColor','blue');
//获取层中所有有name属性别且属性值为name的input标签
//$('#dv input[name=name]').css('backgroundColor', 'blue');
//获取层中所有有name属性但是值不是name的input标签
//$('#dv input[name!=name]').css('backgroundColor', 'blue');
//相当于以name开头的name属性值
//$('#dv input[name^=name]').css('backgroundColor', 'blue');
//以name结尾的
//$('#dv input[name$=name]').css('backgroundColor', 'blue');
//name属性值中只要有name就可以
//$('#dv input[name*=name]').css('backgroundColor', 'blue');
动态创建表格
<script type="text/javascript">
$(function () {
var dic = { "百度": "http://www.baidu.com", "谷歌": "http://www.google.com" };
//创建一个表格
var tb = $('<table border="1"></table>');
for (var key in dic) {
var trObj = $('<tr><td>' + key + '</td><td><a href="' + dic[key] + '">' + key + '</a></td></tr>'); //.appendTo(tb);
tb.append(trObj);
}
$('body').append(tb);
});
</script>
创建层,层中添加元素
<script type="text/javascript">
$(function () {
//创建层按钮
$('#btn').click(function () {
$('<div id="dv"></div>').css({ "width": "300px", "height": "200px", "backgroundColor": "red", "cursor": "pointer" }).appendTo($('body'));
});
//创建层中元素,按钮
$('#btnadd').click(function () {
$('<input type="button" name="name" value="按钮" />').appendTo($('#dv'));
});
//清除元素
$('#btnemp').click(function () {
//清空层中所有子元素
$('#dv').empty();
//层没了
//$('#dv').remove();
});
});
</script>
jquery权限管理,左右移动
<script type="text/javascript">
$(function () {
$('#toAllLeft').click(function () {
$('#se1 option').appendTo($('#se2'));
});
$('#toAllRight').click(function () {
$('#se2 option').appendTo($('#se1'));
});
$('#toRight').click(function () {
$('#se1 option:selected').appendTo($('#se2'));
});
$('#toLeft').click(function () {
$('#se2 option:selected').appendTo($('#se1'));
});
});
</script>
jquery版阅读协议倒计时
<script type="text/javascript">
//十秒钟后协议文本框下的注册按钮才能点击,时钟倒数。设置可用性等jQuery未封装方法:attr("")setInterval()
$(function () {
var sec = 5;
var setId = setInterval(function () {
sec--;
if (sec <= 0) {
$('#btn').attr('disabled', false).val('同意');
//清除计时器
clearInterval(setId);
} else {
$('#btn').val('请仔细阅读协议('+sec+')');
}
}, 1000);
});
</script>
removeAttr与unbind
<script type="text/javascript">
//选择球队,两个ul。被悬浮行高亮显示(背景是红色),点击球队将它放到另一个的球队列表。//清除所有事件。.unbind(); $(function () { $('#uu1 li').mouseover(function () {
$(this).css('backgroundColor', 'red').siblings().css('backgroundColor', '');
}).click(function () {
//removeAttr移除了什么属性
//unbind移除事件
$(this).removeAttr('style').unbind().appendTo($('#uu2')); });
});
</script>
节点替换replaceWith与replaceAll
<script type="text/javascript">
$(function () {
$('#btn1').click(function () {
//把br标签替换成hr标签
$('br').replaceWith('<hr color="yellow" />');
});
$('#btn2').click(function () {
//把hr标签替换成br标签
$('<br />').replaceAll('hr');
});
});
</script>
,jquery版全选,反选,全不选
<script type="text/javascript">
$(function () {
$('#btnAll').click(function () {
$(':checkbox').attr('checked', true);
});
$('#btnNo').click(function () {
$(':checkbox').attr('checked', false);
});
$('#btnFan').click(function () {
$(':checkbox').each(function (k, v) {
$(this).attr('checked', !$(this).attr('checked'));
});
});
});
</script>
jquery绑定与解除事件
<script type="text/javascript">
$(function () {
//注册一个点击事件
$('#btn').bind('click', function () {
alert('哈哈');
}).bind('mouseover', function () {
$(this).css('backgroundColor', 'red');
}); $('#btnClear').click(function () {
$('#btn').unbind('click');//写什么就解除什么事件,什么都不写则全部解除
});
});
</script>
合成事件hover与toggle
<script type="text/javascript">
$(function () {
//合成事件
$('#btn').hover(function () {
//相当于鼠标进来和离开
$(this).css('backgroundColor', 'red');
}, function () {
$(this).css('backgroundColor', 'blue');
});
//事件的切换
//这个事件结束,继续下一个事件,所有事件执行后再从头开始
$('#btn').toggle(function () {
alert('1');
}, function () {
alert('2');
}, function () {
alert('3');
}, function () {
alert('4');
});
}); </script>
jquery版事件冒泡阻止
<script type="text/javascript">
$(function () { $('#dv').click(function () {
alert($(this).attr('id'));
});
$('#p1').click(function () {
alert($(this).attr('id'));
});
$('#sp').click(function (e) {
alert($(this).attr('id'));
e.stopPropagation(); //阻止事件冒泡
});
}); </script>
jquery版时间冒泡阻止默认事件
<script type="text/javascript">
$(function () {
$('#btn').click({ "老牛": "公的" }, function (e) {
alert(e.data.老牛);
}); $('a').click(function (e) {
//alert('不去');
//e.preventDefault();
if (!confirm('去不')) {
e.preventDefault(); //阻止默认事件
// e.stopPropagation(); //阻止事件冒泡
}
});
}); </script>
jquery获取键盘键的值和one
<script type="text/javascript">
$(function () {
$('#txt').keydown(function (e) {
alert(e.which); //获取键盘键的值
});
// $('#txt').mousedown(function (e) {
// alert(e.which);//获取鼠标的左右键的值
// });
//只执行一次
$('#btn').one('click',function () {
alert('哈哈');
});
});
</script>
jquery图片跟随鼠标移动
<script type="text/javascript">
$(function () { $(document).mousemove(function (e) {
$('img').css({ "position": "absolute", "left": e.pageX,"top":e.pageY });
}); }); </script>
juqery动画animate
<script type="text/javascript">
$(function () {
$('img').animate({ "left": "50px", "top": "500px" }, 2000).animate({ "width": "80px", "height": "80px", "top": "-=400px", "left": "500px" }, 2000).animate({ "top": "+=450px", "left": "+=450px" }, 3000);
// $('img').animate({ "left": "50px", "top": "500px" }, 2000).animate({"left":"800px","top":"-50px","width":"200px","height":"200px"}, 3000);
});
</script>
juqery动画版显示隐藏层
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.8.3.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#btn1').click(function () {
$('div').slideDown(2000);
});
$('#btn2').click(function () {
$('div').slideUp(2000);
});
$('#btn3').click(function () {
$('div').slideToggle(2000);
});
$('#btn4').click(function () {
$('div').fadeIn(2000);
});
$('#btn5').click(function () {
$('div').fadeOut(2000);
});
$('#btn6').click(function () {
$('div').fadeToggle(2000);
});
}); </script>
</head>
<body>
<input type="button" name="name" value="slideDown" id="btn1" />
<input type="button" name="name" value="slideUp" id="btn2" />
<input type="button" name="name" value="slideToggle" id="btn3" />
<input type="button" name="name" value="fadeIn" id="btn4" />
<input type="button" name="name" value="fadeOut" id="btn5" />
<input type="button" name="name" value="fadeToggle" id="btn6" />
<div style="background-color: Green; width: 300px; height: 200px">
</div>
</body>
</html>
jquery账户记录cookie
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.8.3.js" type="text/javascript"></script>
<script src="jquery.cookie.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var t = $.cookie("name");
if (t!='') {
$('p').text('欢迎'+t+'登录');
}
$('#btn').click(function () {
$.cookie("name", $('#txt').val());
alert('已经记录了');
//帐号
});
}); </script>
</head>
<body>
<p>欢迎游客登录</p>
<input type="text" name="name" value="" id="txt" />
<input type="button" name="name" value="记录" id="btn" />
</body>
</html>
转自:http://www.cnblogs.com/valiant1882331/p/4071709.html
jQuery小例子的更多相关文章
- Jquery小例子:全选按钮、加事件、挂事件;parent()语法;slideToggle()语法;animate()语法;元素的淡入淡出效果:fadeIn() 、fadeOut()、fadeToggle() 、fadeTo();function(e):e包括事件源和时间数据;append() 方法
function(e): 事件包括事件源和事件数据,事件源是指是谁触发的这个事件,谁就是事件源(div,按钮,span都可以是事件源),时间数据是指比如点击鼠标的事件中,事件数据就是指点击鼠标的左建或 ...
- jQuery小例
jQuery小例子 使用前,请先引用jquery 1,map遍历数组 2,jQuery对象与DOM对象才做元素和互转 3,prevall与nextall 4,jquery版的星星评分控件 5,jq ...
- php+jquery+ajax+json简单小例子
直接贴代码: <html> <title>php+jquery+ajax+json简单小例子</title> <?php header("Conte ...
- bootstrap 模态 modal 小例子
bootstrap 模态 modal 小例子 <html> <head> <meta charset="utf-8" /> <title ...
- Jquery:小知识;
Jquery:小知识: jQuery学习笔记(二):this相关问题及选择器 上一节的遗留问题,关于this的相关问题,先来解决一下. this的相关问题 this指代的是什么 这个应该是比较好理 ...
- 【zTree】 zTree使用的 小例子
使用zTree树不是第一次了 但是 还是翻阅着之前做的 对照着 使用起来比较方便 这里就把小例子列出来 总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先 在 ...
- Jquery小东西收集
1. $(document).ready(),$(function(){}),$(window).load(),window.onload的关系与区别 $(document).ready(functi ...
- bootstrap 模态 modal 小例子【转】
bootstrap 模态 modal 小例子 <html> <head> <meta charset="utf-8" /> <title ...
- 一个jquery ajax例子
上次搞了个jquery的AutoComplete效果,感觉很久没写jquery了,趁热打铁,再找点东西练练手.这不,看了一下jquery手册,顺便写了一个小例子,源码我直接贴上来了. 1.新建一个 ...
随机推荐
- 【二】php常用方法
-------------------------------数据类型------------------------------------------ 1.settype(var,type) 类 ...
- Jmeter测试Mysql
一.在测试计划下,找到Add directory or jar to classpath下填入jdbc驱动路径. 二.新建线程组. 三.在线程组下,添加配置元件—JDBC Connection Con ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- iOS第三方解决键盘遮挡-IQKeyboardManager
百度云:http://pan.baidu.com/s/1yg5ae githun:https://github.com/hackiftekhar/IQKeyboardManager AppDelega ...
- (5)VS2010无法打开gl/glaux.h头文件的解决方法
方法:下载缺少的文件. 参考自:http://blog.csdn.net/delphiwcdj/article/details/6326586 下载源1:CSDN下载OpenGL 安装包所需文件下载: ...
- lintcode :单词搜索
题目 单词搜索 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. 样 ...
- intent传递参数
来建第一个Activity:MyIntent [mw_shl_code=java,true]public class MyIntent extends Activity { ...
- Python模块包中__init__.py文件的作用
转载自:http://hi.baidu.com/tjuer/item/ba37ac4ce7482a0f6dc2f08b 模块包: 包通常总是一个目录,目录下为首的一个文件便是 __init__.py. ...
- swift:入门知识之泛型
在尖括号里写一个名字来创建一个泛型函数或者类型 例如<T>.<Type> 可以创建泛型类.枚举和结构体 在类型后使用where来指定一个需求列表.例如,要限定实现一个协议的类型 ...
- Android 时间轴
最近开发的app中要用到时间轴这东西,需要实现的效果如下: 想想这个东西应该可以用listview实现吧.然后最近就模拟着去写了: 首先写 listview的item的布局: listview_it ...