首页 > Web开发 > 详细

jquery常用方法

时间:2016-01-07 11:31:00      阅读:144      评论:0      收藏:0      [点我收藏+]

一、获取和设置的宽度/高度值

height()/ width()获取第一个匹配元素当前计算的的高度/宽度值。

height(val)/ width(val) 为每个匹配元素设置css高度属性/宽度属性值。

$(#"mydiv").height(); $(#"mydiv").height(10);

二、设置所有匹配元素css样式

css(properties)     把一个“名/值对”对象设置为所有匹配元素的样式属性

---在所有匹配的元素上设置大量样式属性的最佳方式!

$(#"mydiv").css({ height: "10px", background: "blue" });

三、通过animate缓慢改变元素属性

$(".short").animate({width:shortWidth+"px"});

 

 

 

 

 

 

 

 

 

 

 

1、创建一个嵌套的过滤器

.filter(":not(:has(.selected))") //去掉所有不包含class为.selected的元素
3. 使用has()来判断一个元素是否包含特定的class或者元素
 $("input").has(".email").addClass("email_icon");

4. 使用jQuery切换样式

 $(‘link[media=‘screen‘]‘).attr(‘href‘, ‘Alternative.css‘);

6. 如何正确使用ToggleClass

等价写法:

a.hasClass(‘blueButton‘) ? a.removeClass(‘blueButton‘) : a.addClass(‘blueButton‘);
a.toggleClass(‘blueButton‘);

7. 设置IE指定的功能

if ($.browser.msie) { // Internet Explorer is a sadist. }

8. 使用jQuery来替换一个元素

$(‘#thatdiv‘).replaceWith(‘fnuh‘);

9. 验证一个元素是否为空

if ($(‘#keks‘).html()) { //Nothing found ;}

10. 在无序的set中查找一个元素的索引

$("ul > li").click(function () { var index = $(this).prevAll().length; });

11. 绑定一个函数到一个事件

$(‘#foo‘).bind(‘click‘, function() { alert(‘User clicked on "foo."‘); });
 

12. 添加HTML到一个元素

$(‘#lal‘).append(‘sometext‘);

13. 创建元素时使用对象来定义属性

var e = $("", { href: "#", class: "a-class another-class", title: "..." });

14. 使用过滤器过滤多属性

$(‘#someid input[type=sometype][value=somevalue]‘).get();

15. 使用jQuery预加载图片

jQuery.preloadImages = function(){
    for(var i = 0; i).attr(‘src‘, arguments[i]); }
};
     // Usage $.preloadImages(‘image1.gif‘, ‘/path/to/image2.png‘, ‘some/image3.jpg‘);
18. 隐藏包含特定值的元素
$("p.value:contains(‘thetextvalue‘)").hide();

19. 自动的滚动到页面特定区域

jQuery.fn.autoscroll = function(selector) { 
$(‘html,body‘).animate( {scrollTop: $(selector).offset().top}, 500 ); }
 //Then to scroll to the class/area you wish to get to like this: $(‘.area_name‘).autoscroll();

20. 检测各种浏览器

Detect Safari (if( $.browser.safari)),
Detect IE6 and over (if ($.browser.msie && $.browser.version > 6 )), 
Detect IE6 and below (if ($.browser.msie && $.browser.version <= 6 )), 
Detect FireFox 2 and above (if ($.browser.mozilla && $.browser.version >= ‘1.8‘ ))

21. 替换字符串中的单词

 var el = $(‘#id‘); el.html(el.html().replace(/word/ig, ‘‘));

22. 关闭右键的菜单

$(document).bind(‘contextmenu‘,function(e){ return false; });

27. 指定时间后自动隐藏或者关闭元素(1.4支持)

1.3.2用法:

setTimeout(function() { $(‘.mydiv‘).hide(‘blind‘, {}, 500) }, 5000);

1.4用法:

$(".mydiv").delay(5000).hide(‘blind‘, {}, 500);

28. 动态创建元素到DOM

var newgbin1Div = $(‘‘); newgbin1Div.attr(‘id‘,‘gbin1.com‘).appendTo(‘body‘);

29. 限制textarea的字符数量

jQuery.fn.maxLength = function(max){
    this.each(function(){
        var type = this.tagName.toLowerCase();
        var inputType = this.type? this.type.toLowerCase() : null;
        if(type == "input" && inputType == "text" || inputType == "password"){
             //Apply the standard maxLength
            this.maxLength = max; }
        else if(type == "textarea"){ 
            this.onkeypress = function(e){
                var ob = e || event;
                var keyCode = ob.keyCode;
                var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd; 
                return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection); };
                this.onkeyup = function(){
                    if(this.value.length > max){ this.value = this.value.substring(0,max); }
                     }; } }); };
 //Usage: $(‘#gbin1textarea‘).maxLength(500);

 

jquery常用方法

原文:http://www.cnblogs.com/starof/p/4970744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!