jQuery如今已经成为Web开发中最流行的JavaScript库,通过jQuery和大量的插件,你可以轻松实现各种绚丽的效果。本文将为你介绍一些jquery实用的技巧,希望可以帮助你更加高效地使用jQuery。
收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发.
1. 禁止右键点击
$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; });});2. 隐藏搜索文本框文字
Hide when clicked in the search field, the value.(example can be found below in the comment fields)$(document).ready(function() {$("input.text1").val("Enter your search text here"); textFill($('input.text1'));}); function textFill(input){ //input focus text function var originalvalue = input.val(); input.focus( function(){ if( $.trim(input.val()) == originalvalue ){ input.val(''); } }); input.blur( function(){ if( $.trim(input.val()) == '' ){ input.val(originalvalue); } });}3. 在新窗口中打开链接
XHTML 1.0 Strict doesn't allow this attribute in the code, so use this to keep the code valid.$(document).ready(function() { //Example 1: Every link will open in a new window $('a[href^="http://"]').attr("target", "_blank"); //Example 2: Links with the rel="external" attribute will only open in a new window $('a[@rel$='external']').click(function(){ this.target = "_blank"; });});// how to use<a href="http:///ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT>34. 禁用Jquery(动画)效果
$(document).ready(function() { jQuery.fx.off = true;});35. 与其他Javascript类库冲突解决方案
$(document).ready(function() { var $jq = jQuery.noConflict(); $jq('#id').show();});以上内容就是小编给大家分享的程序员必知35个jQuery 代码片段,希望大家喜欢。