最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Restoring elements style using jquery - Stack Overflow

programmeradmin1浏览0评论

Say I have the following css:

.cls {}
.cls ul {list-style-type:none;}
.cls ul li
{
    border-color:#ff0000;
    border-style:solid;
    float:left;
    padding:0px 20px 0px 2px;
    border-left-width:1px;
    border-bottom-width:0px;
    border-top-width:0px;
    border-right-width:0px;
}

I assign the class "cls" to a <div> as follows:

<div class="cls">
<ul>
<li id="foo">Foo</li>
<li id="bar">Bar</li>
</ul>
</div>

If I manipulate element properties using jquery, say I change the border-left-color on the "bar" listitem as follows:

$("#bar").css("border-left-color", "#0000ff");

Is there a "jquery way" to RESTORE the properties that the listitem "bar" had inherited when the containing <div> was initially assigned the class "cls"? Obviously without having to do:

$("#bar").css("border-left-color", "#ffff00"); }. 

Something in the form of, $().restoreClass() or equivalent???

Say I have the following css:

.cls {}
.cls ul {list-style-type:none;}
.cls ul li
{
    border-color:#ff0000;
    border-style:solid;
    float:left;
    padding:0px 20px 0px 2px;
    border-left-width:1px;
    border-bottom-width:0px;
    border-top-width:0px;
    border-right-width:0px;
}

I assign the class "cls" to a <div> as follows:

<div class="cls">
<ul>
<li id="foo">Foo</li>
<li id="bar">Bar</li>
</ul>
</div>

If I manipulate element properties using jquery, say I change the border-left-color on the "bar" listitem as follows:

$("#bar").css("border-left-color", "#0000ff");

Is there a "jquery way" to RESTORE the properties that the listitem "bar" had inherited when the containing <div> was initially assigned the class "cls"? Obviously without having to do:

$("#bar").css("border-left-color", "#ffff00"); }. 

Something in the form of, $().restoreClass() or equivalent???

Share Improve this question asked Jun 17, 2011 at 12:50 John GathogoJohn Gathogo 4,6653 gold badges35 silver badges49 bronze badges 4
  • Most of the ments here seem to indicate that $().removeAttr('[style]'); should do the job. I somehow cant seem to make it work, and before I make any conclusion, I just want to be sure the problem is not the jquery version am running – John Gathogo Commented Jun 17, 2011 at 13:12
  • see my solution.Updated it. I think it can solve your issue. – DhruvPathak Commented Jun 17, 2011 at 13:14
  • please check my solution. Demo included – Jose Rui Santos Commented Jun 17, 2011 at 13:27
  • UPDATE: I have to mention here that the suggestion given by @Jose Rui Santos is a pragmatic approach and ideal. But when that approach does not render readily, you can use the following to restore the inherited style: $('#bar').css({ 'border-left-color': '' }); Somehow, the empty quote restores the previous inherited style. – John Gathogo Commented Jun 17, 2011 at 13:53
Add a ment  | 

6 Answers 6

Reset to default 7

Define a new class

.blueBorder {
 border-left-color: #0000ff;
}

Then you can toggle the style with

$("#bar").toggleClass('blueBorder'); // with blue border
$("#bar").toggleClass('blueBorder'); // without blue border
$("#bar").toggleClass('blueBorder'); // with blue border

This is the best way to toggle style. Keep in mind that you can apply more than one class to a single HTML element, so that you can bine styles together. For example

$("#bar").toggleClass('blueBorder');    // with blue border
$("#bar").toggleClass('redBackground'); // with blue border and red background
$("#bar").toggleClass('blueBorder');    // with red background

You should keep your presentation (css) separated from the behavior (js), so the following is not remended:

$("#bar").css("border-left-color", "#0000ff");

Imagine the work you will have, if you write this a thousand times and later your customer decides to change it to yellow.

Demo here

$("#bar").css("border-left-color", "");

When you use .css it adds a style attribute to the element. As long as there was not a style attribute when the page was rendered, calling $().removeAttr('style') should do what you want.

JsFiddle Example

Try: $("#bar").removeAttr('style')

You can get the particular CSS property and store it in a string. Then you can restore it when you are done with the change. But you have to be careful with the name of CSS property : Here it is working: http://jsfiddle/KgEjr/4/

var myOriginal = "" ;

$('#st').click(
    function() { storeAndChange(); } );


$('#re').click(
    function() { restore(); } );


function storeAndChange()
{
    myOriginal = $("#bar").css("border-left-color");

    $("#bar").css("border-left-color", "#0000ff");
    $("#msg").text("changed");
}


function restore()

{

    $("#bar").css("border-left-color", myOriginal);
    $("#msg").text("restored");


}

Could you store the style in a data attribute and recover it later? This seems to work with your example.

$(function(){
    $(".cls ul li").each(function(){
        $(this).data("defaultStyle",$(this).attr("style") || "");
    });

    $("#foo").css({"border-left-color": "#ff00ff", "font-style": "italic", "background-color": "#efefef"});
    $("#bar").css({"border-left-color": "#0000ff", "font-weight": "bold", "background-color": "#cdcdcd"});

    $(".cls ul li").click(function(){
        $(this).attr("style", $(this).data("defaultStyle")); 
    });
});
发布评论

评论列表(0)

  1. 暂无评论