jquery常用代码集锦
1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 )
|
1
2
3
4
5
|
$.ajaxSetup({ ajaxSettings : { contentType : "application/x-www-form-urlencoded;chartset=UTF-8" }}); |
2. jquery判断元素上是否绑定了事件
|
1
2
3
4
5
|
//jQuery event封装支持判断元素上是否绑定了事件,此方法只适用于jQuery绑定的事件var $events = $("#id").data("events");if ($events && $events["click"]) { // your code} |
3. 被选中的option元素
|
1
|
$('#element').find('option:selected'); |
4. 禁用右键单击上下文菜单
|
1
2
3
|
$(document).bind("contextmenu",function(e){ return false;}); |
5. 禁用文本选择功能
|
1
2
3
|
$(document).bind("selectstart", function() { return false;}); |
6. jquery隔行换色
|
1
2
|
$(".div_row1:even").addClass("background_even"); // 匹配偶数行$(".div_row1:odd").addClass("background_odd"); // 匹配单数行 |
7. 鼠标移入变色,移除还原背景色
|
1
2
3
4
5
|
$(".div_row1").mouseover(function() { $(this).addClass("background_mouseover");}).mouseout(function() { $(this).removeClass("background_mouseover");}); |
8. jquery判断鼠标左键、右键
|
1
2
3
4
5
6
7
|
$("#id").mousedown(function(e) { if (3 == e.which) { alert("右键单击事件"); } else if (1 == e.which) { alert("左键单击事件"); }}); |
9.jquery动态添加元素到DOM中
|
1
2
|
var newDiv = $('<div></div>');newDiv.attr('id', 'myNewDiv').appendTo('body'); |
10.jquery元素居中(屏幕正中间)
|
1
2
3
4
5
6
7
8
|
jQuery.fn.center = function () {return this.each(function(){$(this).css({position:'absolute',top, ( $(window).height() - this.height() ) / 2 + $(window).scrollTop() + 'px',left, ( $(window).width() - this.width() ) / 2 + $(window).scrollLeft() + 'px' });});}// 这样来使用上面的函数: $(element).center(); |
11. 把特定名称的所有元素的值都放到一个数组中
|
1
2
3
4
|
var arr = new Array();$("input[name='xxx']").each(function(){arr.push($(this).val());}); |
12. jquery正则表达式除去HTML标签
|
1
2
3
4
5
6
7
8
9
|
(function($) {$.fn.stripHtml = function() {var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;this.each(function() {$(this).html( $(this).html().replace(regexp,'') );});return $(this);}})(jQuery); // 用法: $('p').stripHtml(); |
13.jquery获得鼠标光标位置x和y
|
1
2
3
4
5
|
$(document).ready(function() {$(document).mousemove(function(e) { $("#mouse").html("X point : " + e.pageX + " | Y " + e.pageY); });}); |
14.jquery检查元素是否存在
|
1
2
3
|
if ( $("#id").length > 0 ) { // it exists} |
15.js倒计时
|
1
2
3
4
5
6
7
8
9
|
var count = 5;countdown = setInterval(function() { $("#mouse").html(count + " 秒后将跳转到百度首页!"); if (count == 0) { clearInterval(countdown); } count--;}, 1000); |
16.jquery回到顶部
|
1
2
3
4
5
6
7
8
9
|
jQuery.fn.autoscrolltoTop = function() { $('html,body').animate({ scrollTop: this.offset().top }, 500 );};// 执行如下代码开始滚动$('#footer').autoscrolltoTop(); |
当然除了这些,还有更多常用的jquery代码等着大家去发掘。
原创文章,转载请注明: 转载自java开发者
本文链接地址: jquery常用代码集锦
jquery常用代码集锦的更多相关文章
- jQuery 常用代码集锦
1. 选择或者不选页面上全部复选框 var tog = false; // or true if they are checked on load $('a').click(function() { ...
- Jquery学习总结(1)——Jquery常用代码片段汇总
1. 禁止右键点击 ? 1 2 3 4 5 $(document).ready(function(){ $(document).bind("contextmenu",fun ...
- jquery常用代码
转自:未找到 以下是jquery中比较常用的一些操作实现方式: $("标签名") //取html元素 document.getElementsByTagName("&qu ...
- jQuery常用代码片段
检测IE浏览器 在进行CSS设计时,IE浏览器对开发者及设计师而言无疑是个麻烦.尽管IE6的黑暗时代已经过去,IE浏览器家族的人气亦在不断下滑,但我们仍然有必要对其进行检测.当然,以下片段亦可用于检测 ...
- WebGIS常用代码集锦
一.普通代码 1.坐标转换 ol.proj.transform(coordinate, source, destination) ol.proj.transform(coordinate, 'EPSG ...
- JQuery常用代码汇总
获取<input />的value $("#id").val( ); 标签间的html $("#id").html('<tr><t ...
- 【js】前端 js/jquery 常用代码和实践
1.获取某天后几天的日期 //d为传入的日期 days为d后面的几天function getAfterDate(d,days){ var dd = new Date(d); dd.setDate(dd ...
- js|jquery常用代码
页面重定位: window.location.replace("http://www.bczs.net"); window.location.href = "http:/ ...
- js及jquery常用代码
1.获取屏幕尺寸 document.documentElement.scrollWidth; document.documentElement.scrollHeight; $(window).widt ...
随机推荐
- C#索引器及示例
public class IndexSeletor<T> where T:struct { private List<T> _listObj; public IndexSele ...
- iOS网络检测
使用之前请从Apple网站下载示例:点此下载 Reachability 中定义了3种网络状态: typedef enum : NSInteger { NotReachable = ,//无网络 Rea ...
- loadrunner之Paramater在负载测试中的数据生成规则
前段时间在做性能测试的时候,基于业务的需求,使用到了Unique Number的参数类型. 脚本的业务是注册以alien开头,后面接数字的用户帐号,填写相关帐号信息.提交企业信息进行审核. 其中用户帐 ...
- iOS手势学习UIGestureRecognizer & cocos2d 手势推荐
iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer // 长按UIPanGestur ...
- Project Euler 101 :Optimum polynomial 最优多项式
Optimum polynomial If we are presented with the first k terms of a sequence it is impossible to say ...
- 图片bmp格式转换为jpg格式
一下代码经过个人测试,可用 注意:将jpg格式的图片重命名为bmp格式,在该代码中是不能转换的,会报空值异常!而且IE10是显示不了这样的图片的 import java.awt.Image; impo ...
- 基于Mongodb的轻量级领域驱动框架(序)
混园子也有些年头了,从各个大牛那儿学了很多东西.技术这东西和中国的料理一样,其中技巧和经验,代代相传(这不是舌尖上的中国广告).转身回头一望,几年来自己也积累了一些东西,五花八门涉猎到各种方向,今日开 ...
- Shell脚本基础II
1.shell算术运算 1)加法 r=`expr 4 + 5`(注意! '4' '+' '5' 这三者之间要有空白) r=$[ 4 + 5 ] r=$(( 4 + 5 )) echo $r 2)乘法 ...
- iOS开发多线程--技术方案
pthread 实现多线程操作 代码实现: void * run(void *param) { for (NSInteger i = 0; i < 1000; i++) { ...
- Golang哲学思想
Golang是一门新语言,经过几年发展,慢慢地也已经被许多大公司认可.最大的特点是速度快,并发性好,与网络的功能结合好,是一门服务端语言,号称“网络时代的新语言”:另外还是一个编译型的Python.不 ...