Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery snippets: Some useful sample code


May 30, 2021 Article blog



jQuery is currently a very popular javascript framework, followed by some useful jQuery sample code snippets collected by the web, and of course you can download jQuery </code >

</pre>

★ use jQuery to switch style sheets

$("link[media='screen']").attr("href""Alternative.css");

★ jQuery detects browser types

(if( $.browser.safari)) (if ($.browser.msie && $.browser.version > 6 )) (if ($.browser.msie && $.browser.version <= 6 )) (if ($.browser.mozilla && $.browser.version >= '1.8' ))

★ jQuery to verify that an element is empty

if ($("#Demo").html()) { //null;}

★ jQuery gets the index value from the collection

$("ul > li").click(function (var index = $(this).prevAll().length; });

★ jQuery selects the selected option element

$("#someElement").find("option:selected");

★ jQuery is a selector binding method

$("table").delegate("td""hover"function(){ $(this).toggleClass("hover"); }); //1.42版后,delegate替代live,因为它们提供了更好的上下文支持

★ jQuery automatically scrolls to an area of the page (think of it as a widget)

jQuery.fn.Autoscroll = function(sel{ $('html,body').animate( {scrollTop: $(sel).offset().top},500 ); } //调用:$("#area_name").Autoscroll();

★ jQuery limits the number of characters in the TextArea field (think of it as a widget)

(function($) { jQuery.fn.maxLength = function(max){ this.each(function(){ var type = this.tagName.toLowerCase(); var inputType = this.type ? this.type.toLowerCase() : nullif (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);

★ jQuery to determine whether an element is visible

if($("#macoArea").is(":visible") == "true") { //少年,别跑 }

★ the jQuery element is centered (can be seen as a widget)

(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();

★ jQuery uses .siblings() to select peer elements

// 少年,你是否这样操作过 $('#nav li').click(function(){ $("#macoArea li").removeClass("current"); $(this).addClass("current"); }); //这样做是不是会更好呢 $("#nav li").click(function(){ $(this).addClass("current").siblings().removeClass("current"); });

★ jQuery action check box is fully selected and counter-selected

var sta = false//你懂,全局东东 $('a').click(function() { $("input[type=checkbox]").attr("checked",!sta); sta = !sta; });

★ jQuery gets the mouse cursor position x and y

$(document).mousemove(function(e)} $(document).ready(function({ $().mousemove(function(e){ $("#macoArea").html("X Axis : " + e.pageX + " | Y Axis " + e.pageY); }); });

★ jQuery parses XML

function ParseXml(xml{ $(xml).find("Node").each(function(){ $("#macoArea").append($(this).attr("Author") + ""); ); }

★ jQuery determines whether the image is fully loaded

$('#demoImg').attr("src""demo.jpg").load(function({ alert("是的,你看到的是真的"); });

★ jQuery lets cookies expire

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

★ jQuery disables the right mouse button

$(function(){ $(document).bind("contextmenu",function(e)return false; }); });