简体   繁体   English

无法提醒第二条警报消息

[英]Unable to alert the second alert message

I'm writing an HTML code where in there is a button, once I click that button, there should be 2 alerts to be shown from JS and here is my code. 我正在编写一个HTML代码,其中有一个按钮,单击该按钮后,应该有2条要从JS显示的警报,这是我的代码。

HTML: HTML:

<button id="chat" class="animated-chat tada" onclick="loadChatbox()" style="position: absolute; bottom: 90px; right: 490px;width: 80px; height: 30px; font-size:25px">Chat</button>
<div class="chatHeader">
  <span class="chat-text" style="text-align: center;">Chat with Care!!</span>
  <div id="close-chat" class="close">&times;</div>
  <div id="minim-chat" onclick="minimChatbox()">
    <span class="minim-button">&minus;</span>
  </div>
  <div id="maxi-chat" onclick="loadChatbox()">
    <span class="maxi-button">&plus;</span>
  </div>
</div>

Javascript: 使用Javascript:

function loadChatbox() {
  alert("Hi");
  alertAgain();
}

function alertAgain() {
  var e = document.getElementById("minim-chat");
  e.style.display = "block";
  var e = document.getElementById("maxi-chat");
  e.style.display = "none";
  var e = document.getElementById("chatbox");
  e.style.margin = "0";
  alert("2nd alert");
}

Here when I click the Chat button, alert("Hi"); 在这里,当我单击“ Chat按钮时, alert("Hi"); is being alerted. 正在收到警报。 but when I comment out the below part. 但是当我注释掉以下部分时。

  var e = document.getElementById("minim-chat");
  e.style.display = "block";
  var e = document.getElementById("maxi-chat");
  e.style.display = "none";
  var e = document.getElementById("chatbox");
  e.style.margin = "0"; 

I can see even alert("2d alert") . 我什至可以看到alert("2d alert") This is really confusing. 这真是令人困惑。 P lease let me know where I am going wrong and how can I fix this. 请让我知道我要去哪里哪里以及如何解决这个问题。

Here is the working fiddle. 这是工作中的小提琴。 https://jsfiddle.net/8kpxwroa/ https://jsfiddle.net/8kpxwroa/

Thanks 谢谢

The second alert is not working because your javascript code is breaking at: 第二个警报不起作用,因为您的JavaScript代码在以下位置中断:

var e = document.getElementById("chatbox");

In your fiddle , if you check the console you will find this 在您的小提琴中 ,如果您检查控制台,您会发现它

在此处输入图片说明

This is because document cant find chatbox id in your code. 这是因为文档无法在您的代码中找到chatbox ID。 So either remove this line 所以要么删除这一行

var e = document.getElementById("chatbox");

or just place a element in your html having chatbox id 或者只是将元素放置在具有chatbox ID的html中

This is causing error 这导致错误

var e = document.getElementById("chatbox");
 e.style.margin = "0";

in your HTML you dont have any tag with "chatbox" id. 在您的HTML中,您没有任何带有“聊天框” ID的标签。 so when it try to execute getting "Cannot read property 'style' of null" check console. 因此,当它尝试执行“无法读取null的属性'style'”时,请检查控制台。

so try to add "chatbox" id or remove it. 因此,请尝试添加“聊天框” ID或将其删除。

I can see even alert("2d alert"). 我什至可以看到alert(“ 2d alert”)。 This is really confusing. 这真是令人困惑。 P lease let me know where I am going wrong and how can I fix this. 请让我知道我要去哪里哪里以及如何解决这个问题。

Based on source-code you have shared, you don't have an element with id chatbox 根据您共享的源代码, 您没有ID为 chatbox的元素

So , document.getElementById("chatbox") returns null and you are getting an error in your browser's console while executing next line 因此, document.getElementById("chatbox")返回null并且在执行下一行时浏览器控制台出现错误

e.style.margin = "0"; 

Since e is null, JS engine will give an error and next statement ( alert ) will not be executed. 由于e为null,因此JS引擎将给出错误,并且下一条语句( alert )将不执行。

Looks like you want to hide the chatheader (updated fiddle ) 看起来您想隐藏chatheader (更新了小提琴

var e = document.querySelector(".chatHeader");
e.style.margin = "0"; 

Your HTML code has no id with name chatbox . 您的HTML代码没有ID,其名称为chatbox So replace it with id chat 因此,将其替换为ID 聊天

var e = document.getElementById("chatbox");

become 成为

var e = document.getElementById("chat");

See below URL : - 请参阅以下网址:-

https://jsfiddle.net/8kpxwroa/14/ https://jsfiddle.net/8kpxwroa/14/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM