How do I toggle CSS with jQuery?
I have the following code:
$('#bc' + $.trim($this) + ' span.dashed-circle').css({ 'border' : '5px solid #000'});
That is triggered by a click.function().
I would like that to be a toggle - so when I click the element, it changes the border to what I have above...but when it is clicked again it disappears or rather sets the border to ' '.
Thoughts?
Edit: I should have been explicit...but I don't want to create a CSS class. The reason being is because when I do that, it messes up the formatting of the element being styled. I am sure that it is some small quirk somewhere that would fix it, but I am not interested in wading through the entire code base 开发者_高级运维to fix little positioning issues with a new class. I would much rather just edit the css attribute directly - because it doesn't interfere with the layout.
Edit2: Here is the jsfiddle of the code I am trying to edit. If you notice, I have the CSS attributes last. But how do I let that be toggled ?
Edit3: If anyone is interested...the UI that this will be used in is for my webapp - http://www.compversions.com
$("trigger-element").toggle( function() {
$(element-to-change).css({ 'border' : '5px solid #000'});
}, function () {
$(element-to-change).css({ 'border' : 'none'});
});
Try This
$(document).ready(function() {
$('#panels tr table tr').toggle(function () {
var $this = $(this),
tr_class = 'dash-elem-selected';
if (!$this.parent('thead').length) {
if ($this.hasClass(tr_class)) {
$this.removeClass(tr_class);
tr_class = 'removeClass';
} else {
$this.addClass(tr_class).siblings().removeClass(tr_class);
tr_class = 'addClass';
}
$this = $this.parents('td table').siblings('.sit-in-the-corner').text();
$('#bc' + $.trim($this) + ' span')[tr_class]('bc-dotted-to-solid');
$('#bc' + $.trim($this) + ' span.dashed-circle').css({ 'border' : '5px solid #000'});
}
}, function() {
//This is your new function that will turn off your border and do any other proccessing that you might need.
$('#bc' + $.trim($this) + ' span.dashed-circle').css({ 'border' : 'none'});
});
});
I would do this by defining a withborder
class and toggling that class.
CSS:
.withborder {
border: 5px solid #000;
}
jQuery:
$('#bc' + $.trim($this) + ' span.dashed-circle').click(function(){
$(this).toggleClass('withborder');
});
you could create a CSS class for this
.specialborderclass {
border: 5px solid #000;
}
Then in javascript toggle the class using jQuery toggleClass() method
$('.yourSelector').toggleClass("specialborderClass");
Use toggle.
$("#IDOfElementYouClickOn").toggle(
function(){$('#bc' + $.trim($this) + ' span.dashed-circle').css({ 'border' : '5px solid #000'});},
function(){$('#bc' + $.trim($this) + ' span.dashed-circle').css({ 'border' : ''});}
);
精彩评论