如何通过 jquery 从 class 中删除 属性?
How can I remove a property from a class via jquery?
我尝试从 "navbar-fixed-top" class":
中删除带有 jquery 的左右 属性
$(document).ready(function () {
var menu = $('.tab-content');
var origOffsetY = menu.offset().top;
function scroll() {
if ($(window).scrollTop() >= origOffsetY) {
$('.tab-content').addClass('navbar-fixed-top');
} else {
$('.tab-content').removeClass('navbar-fixed-top');
}
$('.navbar-fixed-top').removeAttr('right');
$('.navbar-fixed-top').removeAttr('left');
}
但在滚动之后检查器仍然显示 css 如下:
.navbar-fixed-bottom, .navbar-fixed-top {
position: fixed;
right: 0;
left: 0;
z-index: 1030;
}
但我需要它是这样的:
.navbar-fixed-bottom, .navbar-fixed-top {
position: fixed;
z-index: 1030;
}
问题是,此代码来自外部 bootstrap css。所以我不能只更改文件。
试试这个
$(".navbar-fixed-top").css({ 'left' : '', 'right' : '' });
你只需要清空属性
的值
编辑:关于 Jquery 无法访问样式表中的 类 我错了。您只需更改值即可。
$(".navbar-fixed-top").css({ 'left' : 'initial', 'right' : 'initial' });
应该改变 类 属性。
如果您使用空的 ' ',它将不适用于 类。
接受的答案适用于 Chrome 和 Firefox,例如,但它可能会给您在 IE 中带来一些问题,因为它不会将“初始”识别为 CSS 值。
$(".navbar-fixed-top").css({ 'left' : 'initial', 'right' : 'initial' });
如果您也需要在 IE 中进行此操作,您可以尝试优化您的 CSS,这样您就有一个单独的 class 来添加和删除左右,或者尝试'auto' 值,IE 可以识别,但这取决于您的设计。
我尝试从 "navbar-fixed-top" class":
中删除带有 jquery 的左右 属性$(document).ready(function () {
var menu = $('.tab-content');
var origOffsetY = menu.offset().top;
function scroll() {
if ($(window).scrollTop() >= origOffsetY) {
$('.tab-content').addClass('navbar-fixed-top');
} else {
$('.tab-content').removeClass('navbar-fixed-top');
}
$('.navbar-fixed-top').removeAttr('right');
$('.navbar-fixed-top').removeAttr('left');
}
但在滚动之后检查器仍然显示 css 如下:
.navbar-fixed-bottom, .navbar-fixed-top {
position: fixed;
right: 0;
left: 0;
z-index: 1030;
}
但我需要它是这样的:
.navbar-fixed-bottom, .navbar-fixed-top {
position: fixed;
z-index: 1030;
}
问题是,此代码来自外部 bootstrap css。所以我不能只更改文件。
试试这个
$(".navbar-fixed-top").css({ 'left' : '', 'right' : '' });
你只需要清空属性
的值编辑:关于 Jquery 无法访问样式表中的 类 我错了。您只需更改值即可。
$(".navbar-fixed-top").css({ 'left' : 'initial', 'right' : 'initial' });
应该改变 类 属性。
如果您使用空的 ' ',它将不适用于 类。
接受的答案适用于 Chrome 和 Firefox,例如,但它可能会给您在 IE 中带来一些问题,因为它不会将“初始”识别为 CSS 值。
$(".navbar-fixed-top").css({ 'left' : 'initial', 'right' : 'initial' });
如果您也需要在 IE 中进行此操作,您可以尝试优化您的 CSS,这样您就有一个单独的 class 来添加和删除左右,或者尝试'auto' 值,IE 可以识别,但这取决于您的设计。