收集几条jquery的实用技巧如下:
1) 检查IE是否是版本6
- if ( (jQuery.browser.msie) && (parseInt(jQuery.browser.version) < 7) ) {
- $('body').prepend('<div class="warning">You are using an old version of Internet Explorer which is not supported. Please upgrade your browser in order to view this website.</div>');
- }
if ( (jQuery.browser.msie) && (parseInt(jQuery.browser.version) < 7) ) {
$('body').prepend('<div class="warning">You are using an old version of Internet Explorer which is not supported. Please upgrade your browser in order to view this website.</div>');
}
2) 打开一个打印的窗口
- [url=#]Print this page[/url]
- $('a.print').click(function(){
- window.print();
- return false;
- });
[url=#]Print this page[/url]
$('a.print').click(function(){
window.print();
return false;
});
3 禁止表单使用回车键
- $("#form").keypress(function(e) {
- if (e.which == 13) {
- return false;
- }
- });
$("#form").keypress(function(e) {
if (e.which == 13) {
return false;
}
});
4 全选和反选checkbox
- <div class="options">
- [list]
- [*][url=#]Select All[/url]
- [*][url=#]Reset All[/url]
- [/list]
- <input type="checkbox" id="option1" /><label for="option1">Option 1</label>
- <input type="checkbox" id="option2" /><label for="option2">Option 2</label>
- <input type="checkbox" id="option3" /><label for="option3">Option 3</label>
- <input type="checkbox" id="option4" /><label for="option4">Option 4</label>
- </div>
- $('.select-all').live('click', function(){
- $(this).closest('.options').find('input[type=checkbox]').attr('checked', true);
- return false;
- });
- $('.reset-all').live('click', function(){
- $(this).closest('.options').find('input[type=checkbox]').attr('checked', false);
- return false;
- });
<div class="options">
[list]
[*][url=#]Select All[/url]
[*][url=#]Reset All[/url]
[/list]
<input type="checkbox" id="option1" /><label for="option1">Option 1</label>
<input type="checkbox" id="option2" /><label for="option2">Option 2</label>
<input type="checkbox" id="option3" /><label for="option3">Option 3</label>
<input type="checkbox" id="option4" /><label for="option4">Option 4</label>
</div>
$('.select-all').live('click', function(){
$(this).closest('.options').find('input[type=checkbox]').attr('checked', true);
return false;
});
$('.reset-all').live('click', function(){
$(this).closest('.options').find('input[type=checkbox]').attr('checked', false);
return false;
});
5 平均分各个列
有的时候,需要在表格中让各个列等分,可以这样
- var max_height = 0;
- $("div.col").each(function(){
- if ($(this).height() > max_height) { max_height = $(this).height(); }
- });
- $("div.col").height(max_height);
var max_height = 0;
$("div.col").each(function(){
if ($(this).height() > max_height) { max_height = $(this).height(); }
});
$("div.col").height(max_height);
6 将所有的连接用新建窗口打开
- $('a[@rel$='external']').click(function(){
- this.target = "_blank";
- });
- /*
- Usage:
- [url=http://www.catswhocode.com]catswhocode.com[/url]
- */