jQuery是当前非常火爆的一款javascript框架,下边是由网给大家收集的一些实用的jQuery示例代码片段,当然您可以下载jQuery最新版或者阅读jquery中文手册,以便更好的熟悉jquery。
$("link[media='screen']").attr("href", "Alternative.css");
(if( $.browser.safari)) (if ($.browser.msie && $.browser.version > 6 )) (if ($.browser.msie && $.browser.version <= 6 )) (if ($.browser.mozilla && $.browser.version >= '1.8' ))
if ($("#Demo").html()) { //null;}
$("ul > li").click(function () { var index = $(this).prevAll().length; });
$("#someElement").find("option:selected");
$("table").delegate("td", "hover", function(){ $(this).toggleClass("hover"); }); //1.42版后,delegate替代live,因为它们提供了更好的上下文支持
jQuery.fn.Autoscroll = function(sel) { $('html,body').animate( {scrollTop: $(sel).offset().top},500 ); } //调用:$("#area_name").Autoscroll();
(function($) { jQuery.fn.maxLength = function(max){ this.each(function(){ var type = this.tagName.toLowerCase(); var inputType = this.type ? this.type.toLowerCase() : null; if (type == "input" && inputType == "text" || inputType == "password") { //应用标准的maxLength this.maxLength = max; } else if (type == "textarea") { this.onkeypress = function(e){ var ob = e || event; var keyCode = ob.keyCode; var hasSelection = document.selection ? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd; return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection); }; this.onkeyup = function(){ if (this.value.length > max) { this.value = this.value.substring(0, max); } }; } }); })(jQuery); //调用:$('#macoArea").maxLength(500);
if($("#macoArea").is(":visible") == "true") { //少年,别跑 }
(function($) { jQuery.fn.center = function () { this.css('position','absolute'); this.css('top', ( $(window).height() - this.height() ) / +$(window).scrollTop() + 'px'); this.css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + 'px'); return this; } })(jQuery); //调用:$("#macoArea").center();
// 少年,你是否这样操作过 $('#nav li').click(function(){ $("#macoArea li").removeClass("current"); $(this).addClass("current"); }); //这样做是不是会更好呢 $("#nav li").click(function(){ $(this).addClass("current").siblings().removeClass("current"); });
var sta = false; //你懂,全局东东 $('a').click(function() { $("input[type=checkbox]").attr("checked",!sta); sta = !sta; });
$(document).mousemove(function(e)} $(document).ready(function() { $().mousemove(function(e){ $("#macoArea").html("X Axis : " + e.pageX + " | Y Axis " + e.pageY); }); });
function ParseXml(xml) { $(xml).find("Node").each(function(){ $("#macoArea").append($(this).attr("Author") + ""); ); }
$('#demoImg').attr("src", "demo.jpg").load(function() { alert("是的,你看到的是真的"); });
var date = new Date(); date.setTime(date.getTime() + (x * 60 * 1000)); $.cookie("example", "foo", { expires: date });;
$(function(){ $(document).bind("contextmenu",function(e){ return false; }); });