0) { //do }else{ } //判断是鼠标右...">
美文网首页
巧用jquery

巧用jquery

作者: 何必自找失落_ | 来源:发表于2016-11-01 15:31 被阅读0次

//判断有没有元素

if ($("div").length>0) {

//do

}else{

}

//判断是鼠标右键点击还是左键点击  好像右键经常弹窗查看

$("div").click(function(event) {

switch (event.which) {

case 1:

alert('鼠标点击')

break;

case 2:

alert('滑轮点击')

break;

case 2:

alert('右键点击')

break;

default:

alert("意念点击?")

break;

}

});

//这段代码展示了在用户未输入值时,

//如何在文本类型的input域中保留

//一个默认值

$("input:[type='text']").each(function() {

var $trim_val = $(this).val();

var $this = $(this);

$this.focus(function(){

if($this.val()==$trim_val){

$this.val("");

}

}).focusout(function() {

if($.trim($this.val())==""){

//加上trim是因为判断加上的都是空格的时候也让他初始化。可以了解一下trim是干嘛的。

$this.val($trim_val);

}

});

});

//定时隐藏一个元素

setTimeout(function(){

$("div").hide();

},2000);

$("div").delay(2000).animate({"height":"0px"},0);

//1.4版本以上 delay只对动画有用 就是说你这个jq不是动画效果 没法延迟 可以用setTimeout()

//判断是否含有class

if($("#div").hasClass('className')){

//do

}

//判断是否隐藏 true 是可见,false是不可见

if ($("#div").is(":visible")) {

//do

}

//判断选择框是否被选中 radio 也可以这么判断

if ($("input:[type='checkbox']").is(":checked")) {

//do

}

//元素放在屏幕的中心位置 其实不如用position fixed

jQuery.fn.center = function () {

this.css({

"position": 'absolute',

"left":($(window).width()-this.width())/2+$(window).scrollLeft()+'px',

"top":($(window).height()-this.height())/2+$(window).scrollTop()+'px'

});

}

$("#div").center();

$(window).scroll(function(){

$("#div").center();

})

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

$("ul li").click(function(){

$(this).addClass('active').siblings().removeClass('active');

})

//再页面中获得鼠标的位置

$(document).mousemove(function(event) {

console.log("x:"+event.pageX+"  y:"+event.pageY);

});

//检查图片加载完成之后做的事情

$("img").attr("src","images/img1.jpg").load(function(){

//do

})

相关文章

网友评论

      本文标题:巧用jquery

      本文链接:https://www.haomeiwen.com/subject/jegfuttx.html