jQuery 常用代码集锦(必看篇)

jQuery 常用代码集锦(必看篇)

1. 选择或者不选页面上全部复选框

var tog = false; // or true if they are checked on load
$(a).click(function() {
$(“input[type=checkbox]”).attr(“checked”,!tog);
tog = !tog;
});

2. 取得鼠标的X和Y坐标

$(document).mousemove(function(e){
$(document).ready(function() {
$().mousemove(function(e){
$(#XY).html(“Gbin1 X Axis : ” e.pageX ” | Gbin1 Y Axis ” e.pageY);
});
});

3. 判断一个图片是否加载完全

$(#theGBin1Image).attr(src, image.jpg).load(function() {
alert(This Image Has Been Loaded);
});

4. 判断cookie是否激活或者关闭

var dt = new Date();
dt.setSeconds(dt.getSeconds() 60);
document.cookie = “cookietest=1; expires=” dt.toGMTString();
var cookiesEnabled = document.cookie.indexOf(“cookietest=”) != -1;
if(!cookiesEnabled)
{
//cookies have not been enabled
}

5. 强制过期cookie

var date = new Date();
date.setTime(date.getTime() (x * 60 * 1000));
$.cookie(example, foo, { expires: date });

6. 在表单中禁用“回车键”,表单的操作中需要防止用户意外的提交表单

$(“#form”).keypress(function(e) {
if (e.which == 13) {
return false;
}
});

7. 清除所有的表单数据

function clearForm(form) {
// iterate over all of the inputs for the form
// element that was passed in
$(:input, form).each(function() {
var type = this.type;
var tag = this.tagName.toLowerCase(); // normalize case
// its ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == text || type == password || tag == textarea)
this.value = “”;
// checkboxes and radios need to have their checked state cleared
// but should *not* have their value changed
else if (type == checkbox || type == radio)
this.checked = false;
// select elements need to have their selectedIndex property set to -1
// (this works for both single and multiple select elements)
else if (tag == select)
this.selectedIndex = -1;
});
};

8.禁止多次递交表单

$(document).ready(function() {
$(form).submit(function() {
if(typeof jQuery.data(this, “disabledOnSubmit”) == undefined) {
jQuery.data(this, “disabledOnSubmit”, { submited: true });
$(input[type=submit], input[type=button], this).each(function() {
$(this).attr(“disabled”, “disabled”);
});
return true;
}
else
{
return false;
}
});
});

9. 自动将数据导入selectbox中

$(function(){
$(“select#ctlJob”).change(function(){
$.getJSON(“/select.php”,{id: $(this).val(), ajax: true}, function(j){
var options = ;
for (var i = 0; i

© 版权声明

相关文章

暂无评论

none
暂无评论...