• 转载 五个有用的jQuery技巧


    导读:作为轻量级的JS库,jQuery随着JavaScrīpt脚本的大热而备受Web开发者亲睐。下文里技巧实现的效果虽然并不新鲜,但通过jQuery的封装,HTML实现了很大的清洁。清爽简洁又高效的代码任何时候都是开发者所醉心追求的终极目标,也许它简单,但是它能量巨大。一起来看看Michael Dorf推荐给大家的五个非常实用的jQuery技巧。

    这里要介绍jQuery优化系列的五个jQuery技巧,希望能对你有用。

    一、字体大小的调整。

    允许用户在访问站点时能自由地调节页面字体大小,将带来很好的用户体验。下面的代码就是jQuery要告诉我们怎样做到这点的:

    1. //check that the DOM is ready
    2. $(document).ready(function() {
    3. //get the current font size
    4. var originalFontSize = $('html').css('font-size');
    5. //Increase the font size
    6. $(".increaseFont").click(function() {
    7. var currentFontSize = $('html').css('font-size');
    8. var currentFontSizeNumber = parseFloat(currentFontSize, 10);
    9. //increases the font- could be set to a value from
    10. //the user as well
    11. var newFontSize = currentFontSizeNumber*1.2;
    12. $('html').css('font-size', newFontSize);
    13. return false;
    14. });
    15. //Decrease the Font Size
    16. $(".decreaseFont").click(function() {
    17. var currentFontSize = $('html').css('font-size');
    18. var currentFontSizeNum = parseFloat(currentFontSize, 10);
    19. //decreases font. Could be set to a value from
    20. //the user as well
    21. var newFontSize = currentFontSizeNum*0.8;
    22. $('html').css('font-size', newFontSize);
    23. return false;
    24. });
    25. // Reset Font Size
    26. $(".resetFont").click(function(){
    27. $('html').css('font-size', originalFontSize);
    28. });
    29. });

    建立增减字体大小的样式。

    在新窗口打开链接

    为了让访问者尽量停留在自己的站点上,我们通常会设置在新窗口打开所有外部链接。但在XHTML 1.0中,是没有“_blank”标签属性的。在这类情况下,使用以下jQuery技巧就可以避免这个问题,并能在新窗口中打开所有外部链接。

    1. //check that the DOM is ready
    2. $(document).ready(function() {
    3. //select all anchor tags that have http in the href
    4. //and apply the target=_blank
    5. $("a[href^='http']").attr('target','_blank');
    6. });

    这样就行了!现在,所有的外部链接都可以从一个新窗口打开,以便用户留在原页面。如果原页面使用了很多外部文档的链接,如PDF或DOC文件,那我们可以创建一些规则以便在新窗口中加载这些文件。

    交换样式表

    除了允许访问者改变页面字体大小,还可以允许访问者通过选择不同的页面主题样式来感受不同的页面风格。

    1. //check that the DOM is ready
    2. $(document).ready(function() {
    3. $("a.cssSwap").click(function() {
    4. //swap the link rel attribute with the value in the rel
    5. $('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
    6. });
    7. });

    禁用右键

    通常禁用右键是为了防止访问者直接从页面拷贝信息,或者是想创建自己独特的右键功能。当然,不管什么原因,禁用右键可以通过以下代码实现:

    1. //check that the DOM is ready
    2. $(document).ready(function() {
    3. //catch the right-click context menu
    4. $(document).bind("contextmenu",function(e) {
    5. //warning prompt - optional
    6. alert("No right-clicking!");
    7. //cancel the default context menu
    8. return false;
    9. });
    10. });

    jQuery使我们更容易使用右键来对网页进行处理。

    返回顶部链接

    如果页面过长,可能通过增加“返回顶部”的链接来使访问者方便地返回页面顶部。这是一个简单的JavaScript效果,我们可以通过jQuery运用滚动效果增添一点点小技巧。

    1. $(document).ready(function() {
    2. //when the id="top" link is clicked
    3. $('#top').click(function() {
    4. //scoll the page back to the top
    5. $(document).scrollTo(0,500);
    6. }
    7. });

    对于拥有长页面的站点来说,这真是一个必备功能。

    当你成为一个jQuery熟手时,一定会发现更多的诸如此类的开发技巧。加油!

    英文原文:learncomputer.com

    来源:CSDN编译

  • 相关阅读:
    团队作业第五次——Alpha冲刺
    Alpha冲刺——总结
    冲刺随笔
    冲刺随笔——Day_Nine
    冲刺随笔——Day_Eight
    冲刺随笔——Day_Seven
    冲刺随笔——Day_Three
    团队作业第五次——Alpha冲刺
    第06组 Alpha冲刺(1/6)
    第06组 团队Git现场编程实战
  • 原文地址:https://www.cnblogs.com/huangjacky/p/2371217.html
Copyright © 2020-2023  润新知