码迷,mamicode.com
首页 > 其他好文 > 详细

JQ 常见demo

时间:2016-11-03 18:27:03      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:tar   abs   clone   pos   this   demo   jquer   pagex   res   

1

//这段代码展示了在用户未输入值时,
//如何在文本类型的input域中保留
//一个默认值
wap_val = [];
$(".swap").each(function(i){
wap_val[i] = $(this).val();
$(this).focusin(function(){
if ($(this).val() == swap_val[i]) {
$(this).val("");
}
}).focusout(function(){
if ($.trim($(this).val()) == "") {
$(this).val(swap_val[i]);
}});});

2

var el = $(‘#id‘);
el.html(el.html().replace(/word/ig, ‘‘));

3

$(‘button.someClass‘).live(‘click‘, someFunction);
//注意,在jQuery 1.4.2中,delegate和undelegate选项
//被引入代替live,因为它们提供了更好的上下文支持
//例如,就table来说,以前你会用
//.live()
$("table").each(function(){
$("td", this).live("hover", function(){
$(this).toggleClass("hover");
});
});
//现在用
$("table").delegate("td", "hover", function(){
$(this).toggleClass("hover");
});

4

如何把已创建的元素动态地添加到DOM中:

  1. var newDiv = $(‘‘);  
  2.     newDiv.attr(‘id‘,‘myNewDiv‘).appendTo(‘body‘); 

5

var cloned = $(‘#somediv‘).clone(); 

 

6

  1. if($(element).is(‘:visible‘) == ‘true‘) {  
  2.     //该元素是可见的  
  3. }  

7

JQ中定位

  1. jQuery.fn.center = function () {  
  2.     this.css(‘position‘,‘absolute‘);  
  3.     this.css(‘top‘, ( $(window).height() - this.height() ) / +$(window).scrollTop() + ‘px‘);  
  4.     this.css(‘left‘, ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + ‘px‘);  
  5.     return this;  
  6. }  
  7. //这样来使用上面的函数:  
  8. $(element).center();  

8

如何把有着某个特定名称的所有元素的值都放到一个数组中:

var arrInputValues = new Array();
$("input[name=‘table[]‘]").each(function(){
arrInputValues.push($(this).val());
});

 

9

在jQuery中如何使用.siblings()来选择同辈元素


$(‘#nav li‘).click(function(){
$(‘#nav li‘).removeClass(‘active‘);
$(this).addClass(‘active‘);
});
//替代做法是
$(‘#nav li‘).click(function(){
$(this).addClass(‘active‘).siblings().removeClass(‘active‘);
});

 

10

  正反选

     var tog = false;  

  1. $(‘a‘).click(function() {  
  2.     $("input[type=checkbox]").attr("checked",!tog);  
  3.     tog = !tog;  
  4. });  

11  

如何获得鼠标垫光标位置x和y

$(document).ready(function() {
$(document).mousemove(function(e){
$(’#XY’).html(”X Axis : ” + e.pageX + ” | Y Axis ” + e.pageY);
});
});

 

12

如何把整个的列表元素(List Element,LI)变成可点击的

$("ul li").click(function(){
window.location=$(this).find("a").attr("href");
return false;
});

13

如何检查图像是否已经被完全加载进来

$(‘#theImage‘).attr(‘src‘, ‘image.jpg‘).load(function() {
alert(‘This Image Has Been Loaded‘);
});

14

如何检查cookie是否启用

var dt = new Date();
dt.setSeconds(dt.getSeconds() + 60);
document.cookie = "cookietest=1; expires=" + dt.toGMTString();
var cookiesEnabled = document.cookie.indexOf("cookietest=") != -1;
if(!cookiesEnabled) {
//没有启用cookie
}

 

15

如何让cookie过期:

var date = new Date();
date.setTime(date.getTime() + (x * 60 * 1000));
$.cookie(‘example‘, ‘foo‘, { expires: date });

 

JQ 常见demo

标签:tar   abs   clone   pos   this   demo   jquer   pagex   res   

原文地址:http://www.cnblogs.com/wangxiaoshuai0401/p/6027572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!