JQuery website skinning function implementation code_jquery
Since then, I've found many ways to let visitors toggle stylesheets by clicking somewhere with their mouse. But recently I want to write a tutorial on how to write simple code using jQuery to implement it.
I will walk you through the entire process step by step, not only because I want to show you an introduction to jQuery code, but also to reveal some of the advanced features in the jQuery library.
First, the code
$(document).ready (function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
} );
var c = readCookie('style');
if (c) switchStylestyle(c);
});
function switchStylestyle(styleName)
{
$( 'link[@rel*=style][title]').each(function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName ) this.disabled = false;
});
createCookie('style', styleName, 365);
}
The other part not mentioned here is that you will The functions that create and read cookies are seen later.
Familiar opening
$(document).ready(function(){ $('.styleswitch').click(function()... tells jQuery "Find all containing objects as quickly as possible" element named 'styleswitch' and execute a function when they are clicked by the mouse.
Looks good. When the mouse clicks on the pre-specified element, the switchStylestyle function will be called. From now on that is the focus.
What does this sentence mean?
My mind was a little stuck when I saw this code for the first time:
$('link[@rel*=style]').each(function(i) {
After searching on the Internet, I came up empty-handed. Finally, I had to find John Resig, the author of jQuery, and consult him.
He directly gave me the page address of the jQuery website, which explained several jQuery features. The advanced features (xpath) can be used to find and manipulate several elements in the page.
If you have seen these things, you will understand that the meaning of the mysterious code above is to tell jQuery to "find all elements with rel attributes and A link element whose attribute value string contains 'style'".
Let's see how to write a page that contains one main style sheet and two backup style sheets:
We can Seeing that all style sheets contain a rel attribute containing the 'style' string, the result is clear at a glance.
What is the next step? Iterate through all these stylesheet links and execute the code in the next line:
this.disabled = true;if (this.getAttribute('title') == styleName) this.disabled = false;"Disable all styles first table link, and then open any style sheet whose title attribute value is the same as the string passed by the switchStylestyle function."
It’s a handful, but it’s very effective.
Now all we need to ensure is that those stylesheets exist and are valid.
Full code and demo
Since Kelvin Luck has already written this code, I won’t repeat it here.
DEMO
I believe Kelvin got his inspiration from this website, and we can just see if using other tools to implement this function is more complicated and verbose than jQuery.
Complete styleswitch.js
/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/
(function($)
{
$(document).ready(function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
});
var c = readCookie('style');
if (c) switchStylestyle(c);
});
function switchStylestyle(styleName)
{
$('link[@rel*=style][title]').each(function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName) this.disabled = false;
});
createCookie('style', styleName, 365);
}
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime() (days*24*60*60*1000));
var expires = "; expires=" date.toGMTString();
}
else var expires = "";
document.cookie = name "=" value expires "; path=/";
}
function readCookie(name)
{
var nameEQ = name "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i )
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
// /cookie functions

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
