javascript - 通过 DOM 操作更改段落和跨度元素的文本值
问题描述
但是,单击按钮时,事件侦听器无法同时更改引用和作者。我必须注释掉一个才能看到另一个。这是HTML
<div class="quote-box">
<p class="quote-text">The love of money is the root of all evils.
The love of money is the root of all evils.
<span class="author">Author</span>
</p>
<div class="bottom-right">
<button class="quote-btn">CLICK ME</button>
</div>
</div>
这是CSS
*{
background-color: #BBB23E
}
.quote-box{
background-color:#3E95BB;
width:50%;
height:50%;
top:50%;
left:50%;
transform:translate(-50%,-50%);
position:absolute;
margin:0;
}
.quote-box{
display:flex;
align-items:center;
justify-content: center;
}
.quote-text{
position:absolute;
background-color:white;
width:80%;
height:auto;
padding:20px;
}
.author{
border:2px red solid;
}
.bottom-right{
position:absolute;
right:0.5rem;
bottom:0.5rem;
}
这是Javascript
function pickFromArray(choices) {
return choices[Math.floor(Math.random() * choices.length)];
}
let btnEl = document.querySelector("button");
let quoteEl = document.querySelector("p");
let authorEl = document.querySelector("span");
console.log(quoteEl);
btnEl.addEventListener("click", () => {
let radnomQuote = pickFromArray(quotes);
console.log(radnomQuote);
quoteEl.textContent = radnomQuote.quote;
authorEl.innerHTML = radnomQuote.author;
console.log(authorEl.textContent);
})
这是引号数组
const quotes = [
{
quote: "Life isn’t about getting and having, it’s about giving and being.",
author: "Kevin Kruse",
},
{
quote: "Whatever the mind of man can conceive and believe, it can achieve.",
author: "Napoleon Hill",
},
{
quote: "Strive not to be a success, but rather to be of value.",
author: "Albert Einstein",
}
]
我也尝试过innerHTML,但似乎没有任何效果。但是,当我从 p 标签中取出跨度并将绝对位置应用于作者类时,作者出现在白色背景之外,这不是我希望看到的。你能指出我正确的方向吗?
解决方案
问题是设置quoteEl.textContent
有效地删除了作者跨度。相反,设置quoteEl.firstChild.textContent
const quotes = [
{
quote: "Life isn’t about getting and having, it’s about giving and being.",
author: "Kevin Kruse",
},
{
quote: "Whatever the mind of man can conceive and believe, it can achieve.",
author: "Napoleon Hill",
},
{
quote: "Strive not to be a success, but rather to be of value.",
author: "Albert Einstein",
}]
function pickFromArray(choices) {
return choices[Math.floor(Math.random() * choices.length)];
}
let btnEl = document.querySelector("button");
let quoteEl = document.querySelector("p");
let authorEl = document.querySelector("span");
//console.log(quoteEl);
btnEl.addEventListener("click", () => {
let radnomQuote = pickFromArray(quotes);
// console.log(radnomQuote);
quoteEl.firstChild.textContent = radnomQuote.quote;
authorEl.innerHTML = radnomQuote.author;
// console.log(authorEl.textContent);
})
*{
background-color: #BBB23E
}
.quote-box{
background-color:#3E95BB;
width:50%;
height:50%;
top:50%;
left:50%;
transform:translate(-50%,-50%);
position:absolute;
margin:0;
}
.quote-box{
display:flex;
align-items:center;
justify-content: center;
}
.quote-text{
position:absolute;
background-color:white;
width:80%;
height:auto;
padding:20px;
}
.author{
border:2px red solid;
}
.bottom-right{
position:absolute;
right:0.5rem;
bottom:0.5rem;
}
<div class="quote-box">
<p class="quote-text">The love of money is the root of all evils.
The love of money is the root of all evils.
<span class="author">Author</span>
</p>
<div class="bottom-right">
<button class="quote-btn">CLICK ME</button>
</div>
</div>
推荐阅读
- wso2 - 在 wso2 3.0.0 APIM 管理门户中看不到任何警报
- android - Android chrome自定义标签回栈问题
- ruby-on-rails - Rails 测试以在操作前合并控制器
- javascript - 屏幕宽度更改时如何在屏幕上获取 React App 更改内容
- elasticsearch - 具有多个字段的 Elasticsearch 等效 SQL In Query
- javascript - react-native-document-picker 应用程序在文档选择后关闭/崩溃
- java - 空手道:打印语句不使用 log4j2 写入控制台
- asp.net - Blazor Server:如何“监视文件更改”和“附加到 Visual Studio”
- rest - Odoo 中的 Odoo RPC、Xml RPC、JSON RPC 和 REST API 有什么区别?
- firebase - 参数类型“int”不能分配给参数类型“List”
'。使用 FieldValue.arrayUnion Firestore Flutter 时出错