程序员必知35个jQuery 代码片段

程序员必知35个jQuery 代码片段

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 doesnt 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

© 版权声明

相关文章

暂无评论

none
暂无评论...