javascript - 有没有完全去除背景的按钮功能?
问题描述
我在使用按钮功能时遇到问题。我有一个为所有页面设置背景的 HTML 文档(为了保持一个共同的主题)。我在我的一个页面上创建了一个按钮,目的是删除该特定页面上的背景。我使用脚本函数将背景颜色更改为白色。当我按下按钮时,背景变为白色,除了有文字的部分(见下面链接的图片)。我怎样才能让按钮基本上删除整个背景?
这是我到目前为止的代码:
<body>
<style>
body {
background-image: url(IMG_7305.JPG);
background-size: cover;
background-attachment: fixed;
</style>
<button id="button" type="button" onclick="myFunction()"> Press here to remove the background image </button>
<script>
function myFunction() {
document.getElementById("about").style.backgroundColor = "white"
}
</script>
<p>
I want the background to be cleared... even here!!
</p>
解决方案
您可以将其提供给一个类并在单击时删除该类,而不是将您的背景图像放在 css 中的 body 标记上。
<body class='background'>
<style>
.background {
background-image: url(IMG_7305.JPG);
background-size: cover;
background-attachment: fixed;
</style>
<button id="button" type="button" onclick="myFunction()"> Press here to remove the background image </button>
<script>
function myFunction() {
document.body.classList.remove('background')
}
</script>
<p>
I want the background to be cleared... even here!!
</p>
我还建议添加一个容器和样式,而不是 body 标签本身。
推荐阅读
- django - Django:Charfield的get_default显示空字符串
- c - 如何在汇编中表达'\n'?
- r - extrafont::loadfonts(device = "win") 在 Rprofile 中不起作用,但在控制台中很好
- python - Python ValueError 但形状匹配
- python - 为什么我的 kivy 应用程序没有显示内容?
- java - NoNodeAvailableException:没有节点可用于执行查询
- python - 使用 split-apply-combine 通过自定义函数删除一些值并组合剩余的值
- ios - layoutAttributesForElements 上的自定大小左对齐 CollectionView 崩溃
- swift - 如何知道 FMDB 查询是否成功
- ruby-on-rails - <%= f.submit %> 不会将参数发送到控制器。(Ruby On Rails)