Js ---- splice for Array Processing (with the js original addClass method), js ---- addclass

Source: Internet
Author: User

Js ---- splice for Array Processing (with the js original addClass method), js ---- addclass

Last time I wrote a carousel method: http://blog.csdn.net/stronglyh/article/details/46833499

When someone asked me, they gave me html, so I saw jquery referenced in the page, so I wrote it using jquery. Today I have thought about a addClass method in jquery, I don't know if traditional js can be used.

<Html> <style>. font_class {color: # FF0000; font-weight: bold ;}</style> <script language = "javascript"> function addClass (cc) {var obj = document. getElementById ('span _ 1'); var c_class = obj. className; if (c_class = "") {obj. className = cc;} else {// var reg =/^ (. *?) Font_1 (.*?) $/I; // new RegExp ("(^ | \ s)" + className + "(\ s | $ )") var reg = new RegExp ("(^ |. *?) "+ Cc + "(.*?) "); If (c_class.match (reg) {return;} else {c_class + ='' + cc; obj. className = c_class ;}}function removeClass (cc) {var obj = document. getElementById ('span _ 1'); var c_class = obj. className; if (c_class = "") {return;} else {var has_c = false; var a = c_class.split (""); var l =. length; for (var I = 0; I <l; I ++) {if (a [I] = cc) {has_c = true; break ;}} if (has_c) {. splice (I, 1); obj. className =. join ("") ;}} f Unction hasClass (cc) {var obj = document. getElementById ('span _ 1'); var c_class = obj. className; var reg = new RegExp ("(^ |. *?) "+ Cc + "(.*?) "); If (c_class.match (reg) {alert ('this class already exists, ^ _ ^ ');} else {alert ('this class does not exist yet, ^ _ ^ ') ;}}</script> <span id = "span_1" class = "kaishi"> o (prop _ prop) o... haha </span> <br/> <input type = "button" onclick = "addClass ('font _ class '); "value =" click I add class "> <input type =" button "onclick =" removeClass ('font _ class '); "value =" click to delete class "> 

The splice method is used during deletion.

For this method, there are a total of three common usage, detailed explanation:

1. Delete-used to delete an element. There are two parameters: the first parameter (the location of the first item to be deleted) and the second parameter (number of items to be deleted)
2. Insert-insert any element to the specified position of the array. Three parameters: the first parameter (actual location), the second parameter (0), and the third parameter (inserted item)
3. replace-insert any element to the specified position of the array, and delete any number of items, three parameters. The first parameter (start position), the second parameter (number of deleted items), and the third parameter (insert any number of items)

Var lang = ["php", "java", "javascript"]; // Delete var removed = lang. splice (); alert (lang); // php, javascript alert (removed); // java, return Deleted Items // insert var insert = lang. splice (0th, "asp"); // insert alert (insert) from the first position; // return an empty array alert (lang); // asp, php, javascript // replace var replace = lang. splice (, "c #", "ruby"); // delete an item and insert two alert (lang); // asp, c #, ruby alert (replace ); // php, return the deleted item
Now you should know how to use it. Suddenly, jquery is more convenient.

Zookeeper

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.