关闭

26个JQuery使用小技巧(上)

发表于:2010-3-11 10:34

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

  9. 返回页面顶部功能

1. $(document).ready(function() {
2. $('a[href*=#]').click(function() {
3. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
4.  && location.hostname == this.hostname) {
5. var $target = $(this.hash);
6.    $target = $target.length && $target
7.    || $('[name=' + this.hash.slice(1) +']');
8. if ($target.length) {
9. var targetOffset = $target.offset().top;
10.   $('html,body')
11.   .animate({scrollTop: targetOffset}, 900);
12. return false;
13.    }
14.   }
15.   });
16. // how to use
17. // place this where you want to scroll to
18. <A name=top></A>
19. // the link
20. <A href="#top">go to top</A>
21. });

  10.获得鼠标指针XY值

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

  11. 验证元素是否为空

1. $(document).ready(function() {
2. if ($('#id').html()) {
3. // do something
4.    }
5. });

  12. 替换元素

1. $(document).ready(function() {
2.    $('#id').replaceWith('
3. <DIV>I have been replaced</DIV>
4.
5. ');
6. });

  13. jQuery延时加载功能

1. $(document).ready(function() {
2.    window.setTimeout(function() {
3. // do something
4.    }, 1000);
5. });

  14. 移除单词功能

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

相关阅读:

26个JQuery使用小技巧(下)

33/3<123
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号