zoukankan      html  css  js  c++  java
  • 前端常用的几个js判断(二)(转载)

    10.鼠标悬停(hover)切换 class 属性
    假如当用户鼠标悬停在一个可点击的元素上时,你希望改变其效果,下面这段代码可以在其悬停在元素上时添加 class 属性,当用户鼠标离开时,则自动取消该 class 属性:
    $('.btn').hover(function () {
    $(this).addClass('hover');
    }, function () {
    $(this).removeClass('hover');
    });
    你只需要添加必要的CSS代码即可。如果你想要更简洁的代码,可以使用 toggleClass 方法:
    $('.btn').hover(function () {
    $(this).toggleClass('hover');
    });
    注:直接使用CSS实现该效果可能是更好的解决方案,但你仍然有必要知道该方法。

    11.禁用 input 字段
    有时你可能需要禁用表单的 submit 按钮或者某个 input 字段,直到用户执行了某些操作(例如,检查“已阅读条款”复选框)。可以添加 disabled 属性,直到你想启用它时:
    $('input[type="submit"]').prop('disabled', true);
    你要做的就是执行 removeAttr 方法,并把要移除的属性作为参数传入:
    $('input[type="submit"]').removeAttr('disabled');

    12.阻止链接加载
    有时你不希望链接到某个页面或者重新加载它,你可能希望它来做一些其他事情或者触发一些其他脚本,你可以这么做:
    $('a.no-link').click(function (e) {
    e.preventDefault();
    });

    13.切换 fade/slide
    fade 和 slide 是我们在 jQuery 中经常使用的动画效果,它们可以使元素显示效果更好。但是如果你希望元素显示时使用第一种效果,而消失时使用第二种效果,则可以这么做:
    // Fade
    $('.btn').click(function () {
    $('.element').fadeToggle('slow');
    });
    // Toggle
    $('.btn').click(function () {
    $('.element').slideToggle('slow');
    });

    14.简单的手风琴效果
    这是一个实现手风琴效果快速简单的方法:
    // Close all panels
    $('#accordion').find('.content').hide();
    // Accordion
    $('#accordion').find('.accordion-header').click(function () {
    var next = $(this).next();
    next.slideToggle('fast');
    $('.content').not(next).slideUp('fast');
    return false;
    });

    15.让两个 DIV 高度相同
    有时你需要让两个 div 高度相同,而不管它们里面的内容多少。可以使用下面的代码片段:
    var $columns = $('.column');
    var height = 0;
    $columns.each(function () {
    if ($(this).height() > height) {
    height = $(this).height();
    }
    });
    $columns.height(height);
    这段代码会循环一组元素,并设置它们的高度为元素中的最大高。

    16. 验证元素是否为空
    This will allow you to check if an element is empty.
    $(document).ready(function() {
    if ($('#id').html()) {
    // do something
    }
    });

    17. 替换元素
    $(document).ready(function() {
    $('#id').replaceWith('
    <DIV>I have been replaced</DIV>
    ');
    });

    23. jQuery延时加载功能
    $(document).ready(function() {
    window.setTimeout(function() {
    // do something
    }, 1000);
    });

    18. 移除单词功能
    $(document).ready(function() {
    var el = $('#id');
    el.html(el.html().replace(/word/ig, ""));
    });

    19. 验证元素是否存在于jquery对象集合中
    $(document).ready(function() {
    if ($('#id').length) {
    // do something
    }
    });

    20. 使整个DIV可点击
    $(document).ready(function() {
    $("div").click(function(){
    //get the url from href attribute and launch the url
    window.location=$(this).find("a").attr("href"); return false;
    });
    // how to use
    <DIV><A href="index.html">home</A></DIV>

    });

    21. ID与Class之间转换
    当改变Window大小时,在ID与Class之间切换
    $(document).ready(function() {
    function checkWindowSize() {
    if ( $(window).width() > 1200 ) {
    $('body').addClass('large');
    }
    else {
    $('body').removeClass('large');
    }
    }
    $(window).resize(checkWindowSize);
    });

    22. 克隆对象
    $(document).ready(function() {
    var cloned = $('#id').clone();
    // how to use
    <DIV id=id></DIV>

    });

    23. 使元素居屏幕中间位置
    $(document).ready(function() {
    jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
    }
    $("#id").center();
    });

    24. 写自己的选择器
    $(document).ready(function() {
    $.extend($.expr[':'], {
    moreThen1000px: function(a) {
    return $(a).width() > 1000;
    }
    });
    $('.box:moreThen1000px').click(function() {
    // creating a simple js alert box
    alert('The element that you have clicked is over 1000 pixels wide');
    });
    });

    25. 统计元素个数
    $(document).ready(function() {
    $("p").size();
    });

    26. 禁用Jquery(动画)效果
    $(document).ready(function() {
    jQuery.fx.off = true;
    });

    27. 与其他Javascript类库冲突解决方案
    $(document).ready(function() {
    var $jq = jQuery.noConflict();
    $jq('#id').show();
    });

  • 相关阅读:
    最容易忽略的的前端面试基础题目
    关于浮动宽度不够掉盒子的问题解决方法
    最容易忽略的的前端面试基础题目
    构造字典
    Python数据类型---字典
    Python数据类型---列表
    Python数据类型---字符串
    我要学习Python
    [IT练习册]Python练习项目 思路
    【CTF】后续深入学习内容
  • 原文地址:https://www.cnblogs.com/HoverM/p/6210118.html
Copyright © 2011-2022 走看看