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

Native js implements class_javascript techniques to find/add/delete/specify elements

WBOY
Release: 2016-05-16 17:37:16
Original
1060 people have browsed it
Copy code The code is as follows:

window.onload = function(){
var gaga = document .getElementById( "gaga" );
addClass( gaga,"gaga1" )
addClass( gaga,"gaxx" );
removeClass( gaga,"gaga1" )
removeClass( gaga," gaga" )
function hasClass( elements,cName ){
return !!elements.className.match( new RegExp( "(\s|^)" cName "(\s|$)") ); / / ( \s|^ ) Determine whether there is a space in front (s | $) Determine whether there is a space in the back. The two exclamation marks are converted into Boolean values ​​to facilitate judgment
};
function addClass( elements,cName ){
if( !hasClass( elements,cName ) ){
elements.className = " " cName;
};
};
function removeClass( elements,cName ){
if ( hasClass( elements,cName ) ){
elements.className = elements.className.replace( new RegExp( "(\s|^)" cName "(\s|$)" )," " ); // The replace method is to replace
};
};
};
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!