javascript - 如何在按钮单击时更改整个 div 内容
问题描述
我试图通过单击 div 内的按钮来更改 div 的内容。div 包含几个按钮,在每个按钮单击时,我想显示一个带有文本的新 div,通过单击新 div,我想显示原始 div。
这是原始的div:
<div id="services" class="container-fluid text-center">
<div class="col-sm-4">
<button class="glyphicon glyphicon-heart logo-small"></button>
<h4>blalala</h4>
<p>blalala</p>
</div>
</div>
我试图通过单击按钮而不是“服务” div 来显示不同的 div。
解决方案
我希望这有帮助。
我们有一个div
hold 3 buttons
,每个button
都有它自己的文本值。
每次我们单击 a 时,button
我们都会创建一个新div
的 a ,其中包含我们单击p
的值。button
(function(){
function printButtons(){
// array to store the HTML output
const output = [];
// loop to get all the buttons
myBtnsArr.forEach((button) => {
let myBtn = `<button class="${button.class}" id="${button.id}">
${button.content}
</button>`;
//print the results
output.push(myBtn);
buttonsDiv.innerHTML = output.join('');
});
}
// the div we're going to print the buttons in it
const buttonsDiv = document.getElementById('allBtns');
// all the buttons (in an array)
const myBtnsArr = [
{class: 'myBtn', id: 'button1', content: 'button 1'},
{class: 'myBtn', id: 'button2', content: 'button 2'},
{class: 'myBtn', id: 'button3', content: 'button 3'}
];
// Run the function
printButtons();
})();
// Get the button
let myBtn1 = document.getElementById('button1');
let myBtn2 = document.getElementById('button2');
let myBtn3 = document.getElementById('button3');
let myDiv = document.getElementById('myDiv');
// Run the "createdNewDiv" function when clicking on the button, passing the text as an argument
myBtn1.addEventListener('click', () => {
let button1Text = "This is button 1 text";
createNewDiv(button1Text);
});
myBtn2.addEventListener('click', () => {
let button2Text = "This is button 2 text";
createNewDiv(button2Text);
});
myBtn3.addEventListener('click', () => {
let button3Text = "This is button 3 text";
createNewDiv(button3Text);
});
// When calling the functuin....
function createNewDiv(text){
// 1- We create a new div
let theNewDiv = document.createElement('div');
theNewDiv.classList.add('createdDiv');
// 2- We create a new pagaraph to hold the text we passed
let thewNewPagaraph = document.createElement('p');
thewNewPagaraph.classList.add('createdParagraph');
thewNewPagaraph.innerHTML = text;
// append the 'p' to the 'div'
theNewDiv.appendChild(thewNewPagaraph);
// append the new div to the original div
myDiv.appendChild(theNewDiv);
// when clicking on the new div we delete it, then we see the original div back.
let createdDiv = document.querySelector('.createdDiv');
createdDiv.addEventListener('click', () => {
createdDiv.remove();
});
}
#myDiv {
border: 1px solid;
width: 400px;
height: 200px;
padding: 20px 0;
text-align: center;
position: relative;
border-radius: 4px;
}
#allBtns {
display: flex;
justify-content: space-around;
}
.createdDiv {
position: absolute;
width: 100%;
height: 100%;
background-color: #ebebeb;
border-radius: 4px;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
<div id="myDiv">
<div id="allBtns"></div>
<h4 id="myHeading">My h4</h4>
<h4 id="myParagraph">My pagaraph</h4>
</div>
推荐阅读
- ansible - 无法使用 ansible playbook 安装 dnspython
- jquery - 当值与数组对象匹配时,inArray 不返回 true
- c# - 使用 .NET Core 使用 Telerik Reporting 生成 PDF
- g++ - 配置 jellyfish 问题无法使用 MinGW g++ 运行 C 编译程序
- java - 为什么在查询时即使参数是String,String也会被转换为Long
- node.js - 噩梦页面内容未加载 - 无法分配给对象 '# 的只读属性 'onbeforeunload'
' - google-app-engine - PhpPathError: 对于 php72,必须在 Google App Engine 项目中指定 --php_executable_path
- c# - 在 csproj 中,我可以有条件地包含基于运行时标识符的文件吗?
- python - 通过比较不同数据框中的其他两列来连接一列
- redis - 较低的redis哨兵可以与较高的redis实例对话吗?