Jquery常用的功能

1、判断一个元素是否为空
if ($('#id').html()) {
// do something
}

2、替换元素
$('#id').replaceWith('

I have been replaced

');

3、jquery timer 返回函数
$(document).ready(function() {
window.setTimeout(function() {
// do something
}, 1000);
});

4、jquery也玩替换
$(document).ready(function() {
var el= $('#id');
el.html(el.html().replace(/word/ig, ""));
});

5、判断元素是否存在
$(document).ready(function() {
if ($('#id').length) {
// do something
}
});

6、让div也可以click
$("div").click(function(){
//get the url from href attribute and launch the url
window.location=$(this).find("a").attr("href"); return false;
});// how to use


7、使用jquery来判断浏览器大小添加不同的class
$(document).ready(function() {
function checkWindowSize() {
if ( $(window).width() > 1200 ) {
$('body').addClass('large');
}
else {
$('body').removeClass('large');
}
}
$(window).resize(checkWindowSize);
});
8、几个字符就clone!
var cloned= $('#id').clone()
9、设置div在屏幕中央
$(document).ready(function() {
jQuery.fn.center= function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
$("#id").center();
});

10、创建自己的选择器
$(document).ready(function() {
$.extend($.expr[':'], {
moreThen1000px: function(a) {
return $(a).width() > 1000;
}
});
$('.box:moreThen1000px').click(function() {
// creating a simple js alert box
alert('The element that you have clicked is over 1000 pixels wide');
});
});

11、计算元素的数目
$(document).ready(function() {
$("p").size();
});

12、设置自己的li样式
$(document).ready(function() {
$("ul").addClass("Replaced");
$("ul > li").prepend("? ");
// how to use
ul.Replaced { list-style : none; }
});

13、使用google的主机来加载jquery库

// Example 2:(the best and fastest way)

14、关闭jquery动画效果
$(document).ready(function() {
jQuery.fx.off= true;
});

15、使用自己的jquery标识
$(document).ready(function() {
var $jq= jQuery.noConflict();
$jq('#id').show();
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值