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();
});

前端常用的几个js判断(二)的更多相关文章

  1. 前端常用的几个js判断(一)

    1. 禁止右键点击$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return ...

  2. 前端开发必须知道的JS(二) 闭包及应用

    http://www.cnblogs.com/ljchow/archive/2010/07/06/1768749.html 在前端开发必须知道的JS(一) 原型和继承一文中说过下面写篇闭包,加之最近越 ...

  3. 前端工程化-webpack(打包JS)(二)

    一.第一种打包方式 webpack entry<entry> output 假设目录结构如下: index.html是入口文件 打包app.js为bundle.js如下 app.js 当使 ...

  4. 前端常用场景总结CSS/JS/插件(实用篇更新中...)

    <div class="box box1"> <span>垂直居中</span> </div> .box1{ display: ta ...

  5. 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)

    目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...

  6. api日常总结:前端常用js函数和CSS常用技巧

    我的移动端media html{font-size:10px} @media screen and (min-width:321px) and (max-width:375px){html{font- ...

  7. 前端常用功能记录(二)—datatables表格(转)

    前端常用功能记录(二)—datatables表格 并不是所有的后台开发都有美工和前端工程师来配合做页面,为了显示数据并有一定的美感,jQuery的DataTables插件对于像我这样的前端菜鸟来说真是 ...

  8. JS判断客户端是否是iOS或者Android或者ipad(二)

    js判断客户端是IPAD和iphone 多了就不说了,直接上代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22     funct ...

  9. 整理部分JS 控件 WEB前端常用的做成Jsp项目,方便今后直接用

    整理部分JS 控件  WEB前端常用的做成Jsp项目,方便今后直接用 最近又没时间了,等用时间了,再加入更多的, 源码下载: http://download.csdn.net/detail/liang ...

随机推荐

  1. 通达信5分钟.lc5和.lc1文件格式

    一.通达信日线*.day文件    文件名即股票代码    每32个字节为一天数据    每4个字节为一个字段,每个字段内低字节在前    00 ~ 03 字节:年月日, 整型    04 ~ 07 ...

  2. asp.net mvc adminlte第一波

    首页模板选用官方DEMO中的Blank模板,这个模板相对来说是最干净的. 首页模板的分割: 官方文档是分的4个部分 Wrapper .wrapper. A div that wraps the who ...

  3. Submit Text 快捷键总结

    Ctrl+D : 选择单词,重复可增加选择下一个相同的单词Ctrl+F : 查找内容Ctrl+G : 跳转到指定行Ctrl+H : 替换    Ctrl+J : 合并行(已选择需要合并的多行时)Ctr ...

  4. 虚拟机中Linux系统盘空间不足

    虚拟机中Linux系统盘在使用过程中, 出现空间不足的提示. 使用命令du --max-depth=1 -h 查看Home目录下各个文件占用空间, 发现是./cache(隐藏文件)占用很大空间.进入c ...

  5. 。net 之view筛选

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. Oracle 截取字符串

    如下有一个创建函数的代码,是将一穿字符串按照逗号‘,’分割成若干段 create or replace function SplitStringByComma(aName in varchar2) r ...

  7. C#常用开源类库

    一.AOP框架        Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种 ...

  8. 设计一个泛型类Collection

    要求:设计一个泛型类Collection,它存储object对象的集合(在数组中),以及该集合当前的大小.提供public方法isEmtpy,makeEmpty,insert,remove,isPre ...

  9. 高通android开发缩写

    1.TLMM MSM TLMM pinmux controller,Qualcomm MSM integrates a GPIO and Pin mux/config hardware, (TOP L ...

  10. C# API项目代码正确 ,页面出不来的问题

    C# API项目代码正确  页面出不来的问题,截图如下: 解决方法: 在项目里设置好[起始页],就可以了.