JQuery remove styles added

I am new to Javascript, I am using Backbone So I add a style like this(to a link when its clicked)

     $(eventC.target.parentNode).css('border', '1px solid red');

I want the previous link to be cleared of the red border I added when I click on new link

I looked at link below jQuery - remove style added with .css() function

So in essence I want to do this:

$(OLDLINK).css('border', 'none');
$(NewLINK).css('border', '1px solid red');

with events I get from backbone

Is there a simple way to do this? or is this approach just wrong?


I suggest using a CSS class for adding the border and to add / remove it like below

CSS:

.borderRed
{
  border : 1px solid red;
}

jQuery:

//remove border from old link
$('.borderRed').removeClass('borderRed');

// add border in new link
$(eventC.target.parentNode).addClass('borderRed');

If I clearly understand your question, you need something like this

var container = $("div.parent").on("click", "a", function(){
    $(this).addClass("selected");
    container.find("a").not($(this)).removeClass("selected");
});

demo


This solution may be as:

Backbone.View.extend ({

 events: {
   "event-which-delete-border a.link":"changeLink",
   "event-which-add-border a.link":"changeLink"
 },
changeLink:function(event) {

 if (event.type == 'event-which-delete-border') {
   $('a.link').css('border', 'none');
 } else if (event.type == 'event-which-add-border')
   $('a.link').css('border', '1px solid red');
 }

})

In this example, words 'event-which-delete-border' and 'event-which-add-border' may be all events from JavaScript, such as 'click' 'mouseover' and etc

链接地址: http://www.djcxy.com/p/94882.html

上一篇: 彻底删除CSS属性

下一篇: JQuery删除添加的样式