0 Comments

50个必备的实用jQuery代码段(3)

发布于:2013-10-24  |   作者:广州网站建设  |   已聚集:人围观

21. 如何替换串中的词


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

22. 如何禁用右键单击上下文菜单:


  1. $(document).bind('contextmenu',function(e){  
  2.     return false;  
  3. }); 

23. 如何定义一个定制的选择器
广州网站建设,网站建设,广州网页设计,广州网站设计


  1. $.expr[':'].mycustomselector = function(element, index, meta, stack){  
  2. // element- 一个DOM元素  
  3. // index – 栈中的当前循环索引  
  4. // meta – 有关选择器的元数据  
  5. // stack – 要循环的所有元素的栈  
  6. // 如果包含了当前元素就返回true  
  7. // 如果不包含当前元素就返回false };  
  8. // 定制选择器的用法:  
  9. $('.someClasses:test').doSomething(); 

24. 如何检查某个元素是否存在


  1. if ($('#someDiv').length) {  
  2. //万岁!!!它存在……  

25. 如何使用jQuery来检测右键和左键的鼠标单击两种情况:


  1. $("#someelement").live('click'function(e) {  
  2.     if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) {  
  3.         alert("Left Mouse Button Clicked");  
  4.     } else if(e.button == 2) {  
  5.         alert("Right Mouse Button Clicked");  
  6.     }  
  7. }); 

26. 如何显示或是删除input域中的默认值


  1. //这段代码展示了在用户未输入值时,  
  2. //如何在文本类型的input域中保留  
  3. //一个默认值  
  4. wap_val = [];  
  5. $(".swap").each(function(i){  
  6.     wap_val[i] = $(this).val();  
  7.     $(this).focusin(function(){  
  8.         if ($(this).val() == swap_val[i]) {  
  9.             $(this).val("");  
  10.         }  
  11.     }).focusout(function(){  
  12.         if ($.trim($(this).val()) == "") {  
  13.             $(this).val(swap_val[i]);  
  14.         }  
  15.     });  
  16. }); 

27. 如何在一段时间之后自动隐藏或关闭元素(支持1.4版本):


  1. //这是1.3.2中我们使用setTimeout来实现的方式  
  2. setTimeout(function() {  
  3.   $('.mydiv').hide('blind', {}, 500)  
  4. }, 5000);  
  5. //而这是在1.4中可以使用delay()这一功能来实现的方式(这很像是休眠)  
  6. $(".mydiv").delay(5000).hide('blind', {}, 500); 

28. 如何把已创建的元素动态地添加到DOM中:
广州网站建设,网站建设,广州网页设计,广州网站设计


  1. var newDiv = $('');  
  2.     newDiv.attr('id','myNewDiv').appendTo('body'); 

29. 如何限制“Text-Area”域中的字符的个数:


  1. jQuery.fn.maxLength = function(max){  
  2.     this.each(function(){  
  3.         var type = this.tagName.toLowerCase();  
  4.         var inputType = this.type? this.type.toLowerCase() : null;  
  5.         if(type == "input" && inputType == "text" || inputType == "password"){  
  6.             //Apply the standard maxLength  
  7.             this.maxLength = max;  
  8.         }  
  9.         else if(type == "textarea"){  
  10.             this.onkeypress = function(e){  
  11.                 var ob = e || event;  
  12.                 var keyCode = ob.keyCode;  
  13.                 var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;  
  14.                 return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);  
  15.             };  
  16.             this.onkeyup = function(){  
  17.                 if(this.value.length > max){  
  18.                     this.value = this.value.substring(0,max);  
  19.                 }  
  20.             };  
  21.         }  
  22.     });  
  23. };  
  24. //用法  
  25. $('#mytextarea').maxLength(500); 

30. 如何为函数创建一个基本的测试


  1. //把测试单独放在模块中  
  2. module("Module B");  
  3. test("some other test"function() {  
  4.     //指明测试内部预期有多少要运行的断言  
  5.     expect(2);  
  6.     //一个比较断言,相当于JUnit的assertEquals  
  7.     equals( truefalse"failing test" );  
  8.     equals( truetrue"passing test" );  
  9. }); 
标签:
飞机