类似开关的按钮可以创建和删除输入框
Toggle like button to create and remove input box
Am正在创建一个带有一些按钮的HTML页面来创建输入框。按钮的行为应该类似于切换按钮。即,在第一次点击时,输入框应该出现,如果再次点击相同的按钮,则特定的输入框需要消失。按钮切换我已经管理。但是div并没有创建这是我的切换按钮
<button class="btn" id="button_rd" onclick="setColor('button_rd', '#101010')";>one</button>
以下是javascript
var count = 1;
function setColor(btn, color) {
var property = document.getElementById(btn);
if (count == 0) {
property.style.backgroundColor = "#f4543c"//red
property.style.borderColor = "#f4543c"
count = 1;
}
else {
property.style.backgroundColor = "#00a65a"//green
property.style.borderColor = "#008d4c"
count = 0;
var newdiv = '<div class="form-group"><label for="exampleInputEmail1">email</label>'
+'<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email"></div>'
document.getElementById("create").append(newdiv);
}
}
下面是我需要输入框来显示(在这个div内)的地方
<div class="box-body" id="create">
</div>
如果您很乐意使用Jquery,那么您可能正在寻找类似的东西。
与其说是"创建"一个元素,不如说是"切换"其可见性
$(document).ready(function() {
$('[id^=bool]').click(function() {
var id = $(this).attr("id").substr($(this).attr("id").length - 1);
$('[id^=bool' + id + '] .switcher').toggleClass("switched");
var x = $('[id=input' + id + ']').length;
if (x > 0) //there is one there
{
$('[id=input' + id + ']').remove();
} else {
$('body').append('<input type="text" id="input' + id + '" placeholder="input ' + id + '" />');
}
});
});
.bool {
height: 40px;
width: 100px;
background: darkgray;
position: relative;
border-radius: 5px;
overflow: hidden;
box-shadow: inset 5px 0 6px gray;
border: 1px solid black;
}
.bool:before {
content: "On";
left: 10%;
position: absolute;
top: 25%;
}
.bool:after {
content: "Off";
right: 10%;
position: absolute;
top: 25%;
}
.switcher {
height: 100%;
width: 50%;
position: absolute;
background: lightgray;
top: 0;
left: 0;
z-index: 5;
transform: translateX(0px);
transition: all 0.5s;
border-radius: 5px;
box-shadow: inset 0 0 6px black;
}
.switched {
transform: translateX(50px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bool" id="bool1">
<div class="switcher"></div>
</div>
<div class="bool" id="bool2">
<div class="switcher"></div>
</div>
编辑历史
- 根据注释修改了代码段,包括2个切换
- 在Tambo的帮助下重构jquery方法
- 已将标记更改为"append"answers"remove"
OnClick将以下代码写入隐藏:
document.getElementById('create').style.display = 'none';
显示以下代码:
document.getElementById('create').style.display = 'block';
类似:
JavaScript:
<script type="text/javascript">
var count = 1;
function hidShow()
{
if(count == 1)
{
document.getElementById('create').style.display = 'none';
count = 0;
}
else
{
document.getElementById('create').style.display = 'block';
count = 1;
}
}
</script>
HTML:
<button id="button_rd" onClick="hidShow()">one</button>
<div class="box-body" id="create">
<input type="text" id="txt"/>
</div>
而不是
document.getElementById("create").append(newdiv);
"innerHTML"适用于我,如下所示:
document.getElementById("create").innerHTML = '<div class="form-group"><label for="exampleInputEmail1">email</label>'
+'<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email"></div>'
为了删除toggle上的div,我使用了
$('div_id').remove();
相关文章:
- 使用jQuery删除动态创建的元素上的mouseenter事件
- 如何删除我用按钮创建的内容
- 使用删除文本创建新行
- JavaScript:删除动态创建的表
- Rails Production-可以工作,但不能编辑/删除/创建记录
- 删除创建的 JavaScript 字段集
- 在页面加载时创建/设置活动的默认链接,但在单击其他链接时删除活动链接
- 如何动态删除使用 JavaScript 动态创建的输入字段
- Node.js-无法删除使用createWriteStream创建并写入的文件
- 删除在加载dom之后创建的动态元素
- AngularJS-由$scope创建的作用域何时执行$new()已删除.垃圾收集器或Angular负责处理它吗
- 使用FSO创建或删除文件夹
- 如何在 JSP 中的“单击”复选框时创建删除按钮
- 动态创建/删除元素
- Mouseover/out难题上的JS元素创建/删除
- JQueryMobile:当data-role="page"'s JS生成的内容被创建/删除
- 创建删除后撤销选项/功能
- AngularJs表在创建/删除/更新后没有更新
- 使用Plotly.js动态创建/删除/重新设计图形的最佳方式
- jqGrid.不能创建删除按钮