Home > Web Front-end > JS Tutorial > body text

Some useful JavaScript and jQuery snippets to share_jquery

WBOY
Release: 2016-05-16 18:03:13
Original
1091 people have browsed it

Add CSS class to the specified element:

A very clean way to change the look and feel of an element by adding CSS classes instead of adding inline styles. Using jQuery, this is easy to do:

$('#myelement').addClass('myclass');
Copy after login

Remove CSS class from specified element:

You may think that adding CSS classes is awesome, but we also need to know how to remove unnecessary CSS classes. The following code does this:

$('#myelement').removeClass('myclass');
Copy after login

Detects whether the specified element has a certain CSS class:

Being able to detect whether an element has a certain CSS class becomes very useful if your application or website frequently involves adding or removing CSS classes from a given element.

$(id).hasClass(class)
Copy after login

Toggle CSS using jQuery:

As we can see, adding or removing CSS styles from an element using jQuery is very simple and convenient. But what if you want to completely remove the entire CSS file and attach a new style file (such as common page color switching and other effects)? It's actually quite simple, as the following example shows:

$('link[media='screen']').attr('href', 'Alternative.css');
Copy after login

Source: http://addyosmani.com/blog/50-jquery-snippets-for-developers/

Append HTML code to an element:

When you need to append some HTML content to an element, the append() method saves time and effort:

$('#lal').append('sometext');
Copy after login

Detect whether an element exists:

When working with JavaScript, we often need to check whether an element exists. Using jQuery and the length property, it's very simple: if the length is 0, the page does not have that element, otherwise it does.

if ($('img').length) {  log('We found img elements on the page using "img"');} else {  log('No img elements found');}
Copy after login

Source: http://jqueryfordesigners.com/element-exists/

Get the parent element of the specified element:

Working with the DOM you may need to know the direct parent element of an element. The closest() method will let you know:

var id = $("button").closest("div").attr("id");
Copy after login

Source: http://stackoverflow.com/questions/545978/finding-the-id-of-a-parent-div-using-jquery

Get the sibling nodes of an element:

The siblings() method for getting the sibling nodes of an element is a very convenient tool. As shown below, it is very simple to use this method:

$("div").siblings()
Copy after login

Removing an option from a select list:
When using a select list, you may need to update content based on user actions. To remove an option from a select list, use the following code:

$("#selectList option[value='2']").remove();
Copy after login

Source: http://calisza.wordpress.com/2009/03/29/6-jquery-snippets-you-can-use-to-manipulate-select-inputs/

Get the text content of the list option:

This method is useful when you need to quickly detect the option the user selected from a selection menu.

$('#selectList :selected').text();
Copy after login

Apply the "zebra" effect (color change every other row) in the table:

When using tables, a color-changing style is a good solution for better readability. Using jQuery, this is easily possible without any extra HTML markup.

$("tr:odd").addClass("odd");
Copy after login

Source: http://web.enavu.com/tutorials/top-10-jquery-snippets-including-jquery-1-4/

Calculate the number of child nodes of an element:

If you want to see how many div child elements the #foo element contains, the code below will let you know. Simple yet effective!

$("#foo > div").length<br><p>来源:<a href="http://tympanus.net/codrops/2010/01/05/some-useful-javascript-jquery-snippets/" target="_blank">http://tympanus.net/codrops/2010/01/05/some-useful-javascript-jquery-snippets/</a></p>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!