
本文实例总结了经典且实用的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