你可以帮我打开一个模型窗口,点击按钮点击javascript吗? [英] Can u plz help me to open a model window with button click in javascript?

查看:108
本文介绍了你可以帮我打开一个模型窗口,点击按钮点击javascript吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力打开java脚本中的对话框窗口,点击按钮,但我得到一些错误,如无法获取未定义或null引用的属性onclick。你能帮我解决这个问题吗?



我尝试过:



HTML:





Im struggling to open a dialog window in java script with button click, But im getting some error like unable to get property "onclick" of undefined or null reference.Can u plz help me to fix this??

What I have tried:

HTML:


<button id="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">×</span>
    <p>Some text in the Modal..</p>
  </div>

</div>
<pre>

*****js******:
<pre>r modal = document.getElementById('myModal');

var btn = document.getElementById("myBtn");

var span = document.getElementsByClassName("close")[0];
 
btn.onclick = function() {// here I get error 
    modal.style.display = "block";
}
span.onclick = function() {
    modal.style.display = "none";
}


window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

推荐答案

btn将为null \ undefined,因为它无法找到id为myBtn的项目。确保在渲染按钮后js正在执行,因此如果这是一个外部脚本,请确保脚本标签按下按钮,如果它再次直接在页面上,请确保它位于按钮下方。
"btn" will be null\undefined because it can't find an item with an id of "myBtn". Make sure the js is executing after the button has been rendered, so if this is an external script make sure the script tag is blow the button, if it is on the page directly again make sure it is below the button.


这篇关于你可以帮我打开一个模型窗口,点击按钮点击javascript吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆