Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

css - jQuery: Temporarily change a style then reset to original class

Say I have a class named testThing:

.testThing
{
   background-color:#000000;
   float:left;
   height:50px;
   width:50px;
}

And I want to be able to test a background color change to whatever control is of that class on a button click:

function setColor(someColor) 
{
   jQuery('.testThing').css('background-color', someColor);
}

But I want the user to be able to reset to the original color (another button click) based on what the class has:

function resetClass()
{
   jQuery('#currentColor').removeClass('testThing');
   jQuery('#currentColor').addClass('testThing');
}

Seems like this would work (Albiet not the best way to do this) but the control's background color doesn't reset to the original value held in that class.

Now either I need to figure out why that remove to add doesn't reset it OR just a plain better way of doing it... seeing as it seems silly to remove and readd the class...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know this is old, but you can just set the value to an empty string to remove your custom style like so:

// set
$(this).css('background-color', '#000000');

// reset
$(this).css('background-color', '');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...