9. 返回页面顶部功能
[CODE]$(document).ready(function() {
$('a[href*=#]').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
&& location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target
|| $('[name=' + this.hash.slice(1) +']');
if ($target.length) {
var targetOffset = $target.offset().top;
$('html,body')
.animate({scrollTop: targetOffset}, 900);
return false;
}
}
});
// how to use
// place this where you want to scroll to
<A name=top></A>
// the link
<A href="#top">go to top</A>
});[/CODE]
11. 获得鼠标指针XY值
[CODE]$(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>
});[/CODE]
12. 验证元素是否为空
[CODE]$(document).ready(function() {
if ($('#id').html()) {
// do something
}
});[/CODE]
13. 替换元素
[CODE]$(document).ready(function() {
$('#id').replaceWith('
<DIV>I have been replaced</DIV>
');
});[/CODE]
14. jQuery延时加载功能
[CODE]$(document).ready(function() {
window.setTimeout(function() {
// do something
}, 1000);
});[/CODE]
15. 移除单词功能
[CODE]$(document).ready(function() {
var el = $('#id');
el.html(el.html().replace(/word/ig, ""));
});[/CODE]
16. 验证元素是否存在于jquery对象集合中
[CODE]$(document).ready(function() {
if ($('#id').length) {
// do something
}
});[/CODE]