jQuery技术

Jquery下的26个实用小技巧(jQuery tips, tricks solutions)(2)

字号+ 作者:H5之家 来源:H5之家 2015-09-24 15:06 我要评论( )

$(document).ready(function() { $().mousemove(function(e){ //display the x and y axis values inside the div with the id XY $(#XY).html("X Axis : " + e.pageX + " | Y Axis " + e.pageY); }); // how to us


$(document).ready(function() {
$().mousemove(function(e){
//display the x and y axis values inside the div with the id XY
$(#XY).html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
});
// how to use
<DIV id=XY></DIV>
});


11 验证元素是否为空

复制代码 代码如下:


$(document).ready(function() {
if ($(#id).html()) {
// do something
}
});


12 替换元素

复制代码 代码如下:


$(document).ready(function() {
$(#id).replaceWith(
<DIV>I have been replaced</DIV>
);
});


13 jQuery延时加载功能

复制代码 代码如下:


$(document).ready(function() {
window.setTimeout(function() {
// do something
}, 1000);
});


14 移除单词功能

复制代码 代码如下:


$(document).ready(function() {
var el = $(#id);
el.html(el.html().replace(/word/ig, ""));
});


15 验证元素是否存在于Jquery对象集合中

复制代码 代码如下:


$(document).ready(function() {
if ($(#id).length) {
// do something
}
});


16 使整个DIV可点击

复制代码 代码如下:


$(document).ready(function() {
$("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
<DIV><A href="index.html">home</A></DIV>
});


17 ID与Class之间转换当改变Window大小时,在ID与Class之间切换

复制代码 代码如下:


$(document).ready(function() {
function checkWindowSize() {
if ( $(window).width() > 1200 ) {
$(body).addClass(large);
}
else {
$(body).removeClass(large);
}
}
$(window).resize(checkWindowSize);
});


18 克隆对象

复制代码 代码如下:


$(document).ready(function() {
var cloned = $(#id).clone();
// how to use
<DIV id=id></DIV>
});


19 使元素居屏幕中间位置

复制代码 代码如下:


$(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();
});


20 写自己的选择器

复制代码 代码如下:


$(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);
});
});


21 统计元素个数

复制代码 代码如下:


$(document).ready(function() {
$("p").size();
});


22 使用自己的 Bullets

复制代码 代码如下:


$(document).ready(function() {
$("ul").addClass("Replaced");
$("ul > li").prepend("‒ ");
// how to use
ul.Replaced { list-style : none; }
});


23 引用Google主机上的Jquery类库Let Google host the jQuery script for you. This can be done in 2 ways.

复制代码 代码如下:


//Example 1
<SCRIPT src="http://www.google.com/jsapi"></SCRIPT>
<SCRIPT type=text/javascript>
google.load("jquery", "1.2.6");
google.setOnLoadCallback(function() {
// do something
});
</SCRIPT><SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>
// Example 2:(the best and fastest way)
<SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>


24. 禁用Jquery(动画)效果

复制代码 代码如下:


$(document).ready(function() {
jQuery.fx.off = true;
});


25.与其他Javascript类库冲突解决方案

复制代码 代码如下:


$(document).ready(function() {
var $jq = jQuery.noConflict();
$jq(#id).show();
});


26. Load() 函数的运用(页面载入提示):

> 首先写好 CSS,绝对定位到页面右上角 。

复制代码 代码如下:


#loading {
    position:absolute;    z-index:900;text-align:center;
    background-color:#eef2fa;border:1px solid #d8e3e8;
    color:#000;font-size:12px;padding:3px;width:80px;
    right:0;top:0;
}


> 然后用 jQuery 然后在所有图片载入完成之后,隐藏 Loading DIV 。
> 别忘记载入 jQuery 库哈,刚才测试代码的时候地址写错了,差点疯掉 。

复制代码 代码如下:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 7个有用的jQuery小技巧

    7个有用的jQuery小技巧

    2016-02-26 13:02

  • jQuery制作select双向选择列表

    jQuery制作select双向选择列表

    2016-02-26 11:00

  • 全面详细的jQuery常见开发技巧手册

    全面详细的jQuery常见开发技巧手册

    2016-02-26 10:02

  • 强大的jQuery移动插件Top 10

    强大的jQuery移动插件Top 10

    2016-02-25 09:05

网友点评
o