cuốn sách gpt4 ai đã làm

javascript - 在 css 仍然适用的情况下使用 javascript 添加和删除 div 样式

In lại Tác giả: Vũ trụ không gian 更新时间:2023-11-03 21:22:47 27 4
mua khóa gpt4 Nike

作为模板的一部分,我网站的每个页面上都有一个带有特定 ID(“容器”)的 DIV。

#container
{
max-width:980px; min-width:980px; padding-top:6px;
}

单击按钮(切换全屏)时,我会得到客户端支持的最大宽度,然后执行以下操作:

document.getElementById('container').setAttribute('style', 'max-width:' + width + 'px !important;');
document.getElementById('container').setAttribute('style', 'min-width:' + width + 'px !important;');

这工作正常并将最大宽度设置为 1400 像素。

我的问题是,我可以使用 javascript 删除我刚刚添加的样式,这样只有 css 会应用吗?

或者我的选项是否有相同的代码,回到 980?

document.getElementById('container').setAttribute('style', 'max-width:980px !important;');
document.getElementById('container').setAttribute('style', 'min-width:980px !important;');

câu trả lời hay nhất

您可以删除整个 style 属性

 document.getElementById('container').removeAttribute('style');

或者您可以只删除 style 属性中的 chiều rộng tối đa tài sản:

 document.getElementById('container').style.maxWidth = "none";

引用资料:

style

removeAttribute

关于javascript - 在 css 仍然适用的情况下使用 javascript 添加和删除 div 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35916183/

27 4 0
không gian vũ trụ
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress