當我按下按鈕 set backgroundImage = none 時,我無法弄清楚我想要什么,但我無法嘗試這個我的代碼;
我的部分
var colorChanger = document.getElementsByTagName("button")[0];
colorChanger.addEventListener("click", function() {
document.body.style.backgroundColor = "#000";
document.getElementById("home").style.backgroundImage = "none";
#home {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
min-height: 100vh;
align-items: center;
background-image: url(https://via.placeholder.com/200.png);
background-size: cover;
background-position: center;
<section class="home" id="home">
<div class="content">
<h3>Who I am?</h3>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<h2>My Education Process</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<a href="About.html" class="btn">Read More...</a> <br><br></section>
</p>
我也在努力;
var colorChanger = document.getElementsByTagName("button")[0];
colorChanger.addEventListener("click",function() {
document.body.style.backgroundColor = "#000";
document.getElementById("home").style.backgroundImage = null;
兩者都不起作用。
我哪里做錯了?
uj5u.com熱心網友回復:
- 我添加了
#home.bg-none一個比普通 ID 具有更高特定權重的類。否則我們會遇到特定的權重問題,因為 ID 的權重高于類。 - 我
background-image: none;為該類添加了屬性和價值以及您打算進行的background-color更改。 - 我在 JS:
classList.add('class-name')中使用過將類應用于按鈕單擊的部分。
function bgChange() {
var sectionHome = document.querySelector('#home');
sectionHome.classList.add('bg-none');
}
/* css changes */
#home.bg-none {
background-image: none;
background-color: #000;
color: white;
}
/* original CSS, changed ID to class */
#home {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
min-height: 100vh;
align-items: center;
background-image: url(https://via.placeholder.com/200.png);
background-size: cover;
background-position: center;
}
/* for demonstrations purpose only */
body {
margin: 0;
}
#home {
justify-content: center;
font-size: 3em;
}
button {
position: fixed;
top: 10px;
left: 10px;
background-color: grey;
}
<section id="home" class="home">Test-Section</section>
<button onclick="bgChange()">Click me</button>
uj5u.com熱心網友回復:
您可以使用 removeAttribute() 和 setAttribute() 這里是一個例子
var colorChanger = document.getElementsByTagName("button")[0];
colorChanger.onclick = function(){
document.getElementById("home").removeAttribute("class");
document.getElementById("home").setAttribute("class", "homecss");
}
uj5u.com熱心網友回復:
function removeBG(){
event.preventDefault();
document.getElementById("home").style.background = "none";
}
#home{
display: flex;
flex-wrap: wrap;
gap:1.5rem;
min-height: 100vh;
align-items: center;
background-image: url(https://lh3.googleusercontent.com/proxy/YID679VxCfxmJqd8YiF-E43a5XxxTC7R3dE5iGScHPUbMnDvG9-qAG1PEp9Qrl09pmQIzujGCEIpyAyQb25DWosqSTXPoRGcwihJ3wf-NCk4Mhunlwod-EOsoiRMgNuvzh7_if3sfitCBi4LBoVP8qPB);
background-size: cover;
background-position: center;
}
<section class="home" id="home" > <div class="content">
<h3>Who I am?</h3>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<h2>My Education Process</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<a href="about.html" class="btn" onclick="removeBG()">Read More...</a> <br><br></section>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/339883.html
標籤:javascript html 查询 css
上一篇:拆分字串并將其插入到不同的輸入中
下一篇:將資訊從AJAX傳遞到控制器類
