javascript onclick create(element)div viz弹出框
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript onclick create(element)div viz弹出框,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1781字,纯文字阅读大概需要3分钟。
内容图文
![javascript onclick create(element)div viz弹出框](/upload/InfoBanner/zyjiaocheng/697/a06151def5f24e88beff7fd545198af3.jpg)
我正在尝试制作一个弹出框,单击一个按钮就会调用它,这是我到目前为止所拥有的… http://jsfiddle.net/WGPhG/2/
解决方法:
这是一个小提琴,实际上做你想要的 – http://jsfiddle.net/WGPhG/6/
JS
function popUp(){
var popup = document.createElement('div');
popup.className = 'popup';
popup.id = 'test';
var cancel = document.createElement('div');
cancel.className = 'cancel';
cancel.innerHTML = 'close';
cancel.onclick = function (e) { popup.parentNode.removeChild(popup) };
var message = document.createElement('span');
message.innerHTML = "This is a test message";
popup.appendChild(message);
popup.appendChild(cancel);
document.body.appendChild(popup);
}
笔记
要在元素上设置类,可以使用element.className而不是element.class.
对于cancel元素上的onclick事件处理程序,最好直接为onclick处理程序分配一个匿名函数,该函数可以执行上面的示例中所需的操作.
编辑(更有效的方式)
这实际上是获得所需结果的好得多,因为每次显示弹出窗口时都不会有重新创建元素的开销.小提琴 – http://jsfiddle.net/WGPhG/7/
CSS
.popup
{
position:absolute;
top:0px;
left:0px;
margin:100px auto;
width:200px;
height:150px;
font-family:verdana;
font-size:13px;
padding:10px;
background-color:rgb(240,240,240);
border:2px solid grey;
z-index:100000000000000000;
display:none
}
.cancel
{
display:relative;
cursor:pointer;
margin:0;
float:right;
height:10px;
width:14px;
padding:0 0 5px 0;
background-color:red;
text-align:center;
font-weight:bold;
font-size:11px;
color:white;
border-radius:3px;
z-index:100000000000000000;
}
HTML
<button onClick="openPopup();">click here</button>
<div id="test" class="popup">
This is a test message
<div class="cancel" onclick="closePopup();"></div>
</div>
JS
function openPopup() {
document.getElementById('test').style.display = 'block';
}
function closePopup() {
document.getElementById('test').style.display = 'none';
}
内容总结
以上是互联网集市为您收集整理的javascript onclick create(element)div viz弹出框全部内容,希望文章能够帮你解决javascript onclick create(element)div viz弹出框所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。