26 个 JQuery 使用小技巧(jQuery tips, tricks & solutions)

来源:互联网 发布:linux启动服务所有命令 编辑:程序博客网 时间:2024/05/02 01:56

The use of the jQuery library is growing and growing(just released jQuery 1.4), more and more people are using this useful javascript library. This means that more and more useful jQuery tips, tricks and solutions are coming available. That’s why i created a small list of 26 cool and useful jQuery tips, tricks and solutions. 

        jQuery has a lot of very useful plugins  that can help you with almost anything, but there are a couple of plugins that aren’t that smart to use, why? Well sometimes 1 or 2 lines of jQuery code can do the same thing(or even better), so why use a big script if you can do the same trick with a small piece of code. 

        As many of you already know, jQuery can do a lot of things in more than just one way, so if you see a tip, trick or solution and think/know this can be done better, smarter or faster please let me know, post and share it in a comment below of just email me this, so that i can use this for part 2. 

 

1. Disable right-click(禁止右键点击) 

   Disable right-click contextual menu

 

 

2. Disappearing search field text(隐藏搜索文本框文字) 

    Hide when clicked in the search field, the value.(example can be found below in the comment fields)

 

 

3.0pening links in a new window(在新窗口中打开链接) 

   XHTML 1.0 Strict doesn’t allow this attribute in the code, so use this to keep the code valid. 

 

 

4. Detect browser(检测浏览器) 

    Change/add something for a certain browser. 
     Notice: As of jQuery 1.4, the $.browser variable is replaced by $.support. 

 

 

 

 5. Preloading images(预加载图片) 

 This piece of code will prevent the loading of all images, which can be useful if you have a site with lots of images. 

 

 

6. CSS Styleswitcher(页面样式切换) 

    Switch between different styles? 

 

 

7. Columns of equal height(列高度相同) 

    If you are using two CSS columns, use this to make them exactly the same height

 

 

8. Font resizing(动态控制页面字体大小) 

     Want to let the users change there font size? 

 

 

9. Smooth(animated) page scroll(返回页面顶部功能) 

     For a smooth(animated) ride back to the top(or any location). 

 

 

10. Get the mouse cursor x and y axis(获得鼠标指针 X/Y 值) 

       Want to know where your mouse cursor is? 

 

 

11. Verify if an Element is empty(验证元素是否为空) 

       This will allow you to check if an element is empty. 

 

 

12. Replace a element(替换元素) 

      Want to replace a div, or something else? 

 

 

13.jQuery timer callback functions(jQuery 延时加载功能) 

     Want to delay something? 

 

 

 

14.  Remove a word(移除单词功能) 

       Want to remove a certain word(s)? 

 

 

 

15. Verify that an element exists in jQuery(验证元素是否存在) 

       Simply test with the .length property if the element exists. 

 

 

 

16.  Make the entire DIV clickable(使整个 DIV 可点击) 

        Want to make the complete div clickable?

 

 

17. Switch between classes or id’s when resizing the window(id 与 class 之间转换) 

      Want to switch between a class or id, when resizing the window? 

 

 

 

18. Clone a object(克隆对象) 

       Clone a div or an other element. 

 

 

 

19. Center an element on the screen(使元素居屏幕中间位置) 

       Center an element in the center of your screen. 

 

 

 

20. Write our own selector(写自己的选择器) 

      Write your own selectors. 

 

 

 

21. Count a element(统计元素个数)

      Count an element

 

 

 

22. Use Your Own Bullets(使用自己的 Bullets) 

      Want to use your own bullets instead of using the standard or images bullets? 

 

 

 

23. Let Google host jQuery for you(引用 Google 主机上的 jQuery 类库) 

      Let Google host the jQuery script for you. This can be done in 2 ways. 

 

 

 

 

24. Disable jQuery animations(禁用 jQuery 动画效果) 

       Disable all jQuery effects. 

 

 

 

25. No conflict-mode(与其他 Javascript 类库冲突解决方案) 

      To avoid conflict other libraries on your website, you can use this jQuery Method, and assign a different variable name instead of the dollar sign.