职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1027|回复: 5

常用jquery使用技巧

[复制链接]
爱车车 发表于 2011-10-6 09:49 | 显示全部楼层 |阅读模式
一、ajax的应用
  1. $.ajax({url: 'stat.php',   
  2.   
  3. type: 'POST',   
  4.   
  5. data:{Name:"keyun"},   
  6.   
  7. dataType: 'html',   
  8.   
  9. timeout: 1000,   
  10.   
  11. error: function(){alert('Error loading PHP document');},   
  12.   
  13. success: function(result){alert(result);}   
  14.   
  15. });   

  16. $.ajax({url: 'stat.php',

  17. type: 'POST',

  18. data:{Name:"keyun"},

  19. dataType: 'html',

  20. timeout: 1000,

  21. error: function(){alert('Error loading PHP document');},

  22. success: function(result){alert(result);}

  23. });
复制代码
二、禁止鼠标右键
  1. $(document).ready(function(){   
  2. $(document).bind("contextmenu",function(e){   
  3. return false;   
  4. });   
  5. });   

  6. $(document).ready(function(){
  7. $(document).bind("contextmenu",function(e){
  8. return false;
  9. });
  10. });
复制代码
三、隐藏搜索框文本
  1. $(document).ready(function() {   
  2. $("input.text1").val("Enter your search text here");   
  3. textFill($('input.text1'));   
  4. });   
  5. function textFill(input){ //input focus text function   
  6. var originalvalue = input.val();   
  7. input.focus( function(){   
  8. if( $.trim(input.val()) == originalvalue ){ input.val(''); }   
  9. });   
  10. input.blur( function(){   
  11. if( $.trim(input.val()) == '' ){ input.val(originalvalue); }   
  12. });   
  13. }   

  14. $(document).ready(function() {
  15. $("input.text1").val("Enter your search text here");
  16. textFill($('input.text1'));
  17. });
  18. function textFill(input){ //input focus text function
  19. var originalvalue = input.val();
  20. input.focus( function(){
  21. if( $.trim(input.val()) == originalvalue ){ input.val(''); }
  22. });
  23. input.blur( function(){
  24. if( $.trim(input.val()) == '' ){ input.val(originalvalue); }
  25. });
  26. }
复制代码
四、返回页面顶部
  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. });   

  22. $(document).ready(function() {
  23. $('a[href*=#]').click(function() {
  24. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  25. && location.hostname == this.hostname) {
  26. var $target = $(this.hash);
  27. $target = $target.length && $target
  28. || $('[name=' + this.hash.slice(1) +']');
  29. if ($target.length) {
  30. var targetOffset = $target.offset().top;
  31. $('html,body')
  32. .animate({scrollTop: targetOffset}, 900);
  33. return false;
  34. }
  35. }
  36. });
  37. // how to use
  38. // place this where you want to scroll to
  39. <A name=top></A>
  40. // the link
  41. <A href="#top">go to top</A>
  42. });
复制代码
五、延时加载功能
  1. $(document).ready(function() {   
  2. window.setTimeout(function() {   
  3. // do something   
  4. }, 1000);   
  5. });   

  6. $(document).ready(function() {
  7. window.setTimeout(function() {
  8. // do something
  9. }, 1000);
  10. });
复制代码
六、使元素居于屏幕中间
  1. $(document).ready(function() {   
  2. jQuery.fn.center = function () {   
  3. this.css("position","absolute");   
  4. this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");   
  5. this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");   
  6. return this;   
  7. }   
  8. $("#id").center();   
  9. });   

  10. $(document).ready(function() {
  11. jQuery.fn.center = function () {
  12. this.css("position","absolute");
  13. this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
  14. this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
  15. return this;
  16. }
  17. $("#id").center();
  18. });
复制代码
七、引用google主机上的jquery类库
  1. <SCRIPT src="[img]http://www.google.com/jsapi"></SCRIPT>   
  2. <SCRIPT type=text/javascript>   
  3. google.load("jquery", "1.2.6");   
  4. google.setOnLoadCallback(function() {   
  5. // do something   
  6. });   
  7. </SCRIPT><SCRIPT src="[img]http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>   
  8. // Example 2:(the best and fastest way)   
  9. <SCRIPT src="[img]http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>   

  10. <SCRIPT src="[img]http://www.google.com/jsapi"></SCRIPT>
  11. <SCRIPT type=text/javascript>
  12. google.load("jquery", "1.2.6");
  13. google.setOnLoadCallback(function() {
  14. // do something
  15. });
  16. </SCRIPT><SCRIPT src="[img]http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>
  17. // Example 2:(the best and fastest way)
  18. <SCRIPT src="[img]http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>
复制代码
希望以上内容在大家在JQUERY的日常使用中能够起到一定的作用。



钰云 发表于 2011-10-6 09:49 | 显示全部楼层
建议使用最新的1.6.2,
不过你的某些功能在1.6.2下可能无法使用


fl 发表于 2011-10-6 09:49 | 显示全部楼层
BoneWG 写道
建议使用最新的1.6.2,
不过你的某些功能在1.6.2下可能无法使用


谢谢了。还没注意版本问题,后续会注意的

能文能武 发表于 2011-10-6 09:49 | 显示全部楼层
谢谢,提供资源。

就是不少了点啦,呵

愚人 发表于 2011-10-6 09:50 | 显示全部楼层
不错,,谢谢分享

yoyo 发表于 2011-10-6 09:50 | 显示全部楼层
第6个有问题。

如果元素在body下面没有问题。
如果元素的祖先中没有position不是static的元素也没问题。(没设置相当于static)
如果元素的祖先中有position不是static的元素则会有问题的。
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-28 15:13 , Processed in 0.133565 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表