聊天机器人弹出窗口在我点击按钮时未出现。

huangapple go评论182阅读模式
英文:

Chat-bot Pop Up not appearing when I click on Button

问题

以下是您提供的JavaScript代码的中文翻译部分:

class Chatbox {
  constructor() {
    this.args = {
      openButton: document.querySelector(".chatbox__button"),
      sendButton: document.querySelector(".send__button"),
    };

    this.state = false;
    this.messages = [];
  }

  toggleState(chatBox) {
    chatBox.classList.toggle("chatbox--active");
  }

  onSendButton(chatBox) {
    var textField = chatBox.querySelector("input");
    let text1 = textField.value;
    if (text1 === "") {
      return;
    }

    let msg1 = { name: "User", message: text1 };
    this.messages.push(msg1);

    fetch($SCRIPT_ROOT + "/predict", {
      method: "POST",
      body: JSON.stringify({ message: text1 }),
      mode: "cors",
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((r) => r.json())
      .then((r) => {
        let msg2 = { name: "Sam", message: r.answer };
        this.messages.push(msg2);
        this.updateChatText(chatBox);
        textField.value = "";
      })
      .catch((error) => {
        console.error("Error:", error);
        this.updateChatText(chatBox);
        textField.value = "";
      });
  }

  updateChatText(chatBox) {
    var html = "";
    this.messages
      .slice()
      .reverse()
      .forEach(function (item, index) {
        if (item.name === "Sam") {
          html +=
            '<div class="messages__item messages__item--visitor">' +
            item.message +
            "</div>";
        } else {
          html +=
            '<div class="messages__item messages__item--operator">' +
            item.message +
            "</div>";
        }
      });

    const chatMessage = chatBox.querySelector(".chatbox__messages");
    chatMessage.innerHTML = html;
  }

  display() {
    const { openButton, sendButton } = this.args;
    const chatBox = document.querySelector(".chatbox");

    openButton.addEventListener("click", () => this.toggleState(chatBox));

    sendButton.addEventListener("click", () => this.onSendButton(chatBox));

    const inputNode = chatBox.querySelector("input");
    inputNode.addEventListener("keyup", (event) => {
      if (event.key === "Enter") {
        this.onSendButton(chatBox);
      }
    });
  }
}

const chatbox = new Chatbox();
chatbox.display();

希望这有助于您解决聊天框在按钮点击时不显示的问题。如果您需要更多帮助,请告诉我。

英文:

I am currently following a YouTube tutorial on how to build a chatbot using Python and JavaScript. I have managed to implement the chat button on my website, but when I click it, the chatbot pop-up does not appear as expected.

I have provided the relevant code below, here is app.js:

class Chatbox {
  constructor() {
    this.args = {
      openButton: document.querySelector(&quot;.chatbox__button&quot;),
      sendButton: document.querySelector(&quot;.send__button&quot;),
    };

    this.state = false;
    this.messages = [];
  }

  toggleState(chatBox) {
    chatBox.classList.toggle(&quot;chatbox--active&quot;);
  }

  onSendButton(chatBox) {
    var textField = chatBox.querySelector(&quot;input&quot;);
    let text1 = textField.value;
    if (text1 === &quot;&quot;) {
      return;
    }

    let msg1 = { name: &quot;User&quot;, message: text1 };
    this.messages.push(msg1);

    fetch($SCRIPT_ROOT + &quot;/predict&quot;, {
      method: &quot;POST&quot;,
      body: JSON.stringify({ message: text1 }),
      mode: &quot;cors&quot;,
      headers: {
        &quot;Content-Type&quot;: &quot;application/json&quot;,
      },
    })
      .then((r) =&gt; r.json())
      .then((r) =&gt; {
        let msg2 = { name: &quot;Sam&quot;, message: r.answer };
        this.messages.push(msg2);
        this.updateChatText(chatBox);
        textField.value = &quot;&quot;;
      })
      .catch((error) =&gt; {
        console.error(&quot;Error:&quot;, error);
        this.updateChatText(chatBox);
        textField.value = &quot;&quot;;
      });
  }

  updateChatText(chatBox) {
    var html = &quot;&quot;;
    this.messages
      .slice()
      .reverse()
      .forEach(function (item, index) {
        if (item.name === &quot;Sam&quot;) {
          html +=
            &#39;&lt;div class=&quot;messages__item messages__item--visitor&quot;&gt;&#39; +
            item.message +
            &quot;&lt;/div&gt;&quot;;
        } else {
          html +=
            &#39;&lt;div class=&quot;messages__item messages__item--operator&quot;&gt;&#39; +
            item.message +
            &quot;&lt;/div&gt;&quot;;
        }
      });

    const chatMessage = chatBox.querySelector(&quot;.chatbox__messages&quot;);
    chatMessage.innerHTML = html;
  }

  display() {
    const { openButton, sendButton } = this.args;
    const chatBox = document.querySelector(&quot;.chatbox&quot;);

    openButton.addEventListener(&quot;click&quot;, () =&gt; this.toggleState(chatBox));

    sendButton.addEventListener(&quot;click&quot;, () =&gt; this.onSendButton(chatBox));

    const inputNode = chatBox.querySelector(&quot;input&quot;);
    inputNode.addEventListener(&quot;keyup&quot;, (event) =&gt; {
      if (event.key === &quot;Enter&quot;) {
        this.onSendButton(chatBox);
      }
    });
  }
}

const chatbox = new Chatbox();
chatbox.display();

The display() method is responsible for attaching event listeners to the chat button and handling the chatbox toggle functionality. However, when I click the button, nothing happens.

Here is an image of my website:

Here is my html code base for 'base.html':

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url_for(&#39;static&#39;, filename=&#39;style.css&#39;) }}&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Chatbot&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;chatbox&quot;&gt;
        &lt;div class=&quot;chatbox__support&quot;&gt;
            &lt;div class=&quot;chatbox__header&quot;&gt;
                &lt;div class=&quot;chatbox__image--header&quot;&gt;
                    &lt;img src=&quot;https://img.icons8.com/color/48/000000/circled-user-female-skin-type-5--v1.png&quot; alt=&quot;image&quot;&gt;
                &lt;/div&gt;
                &lt;div class=&quot;chatbox__content--header&quot;&gt;
                    &lt;h4 class=&quot;chatbox__heading--header&quot;&gt;Chat support&lt;/h4&gt;
                    &lt;p class=&quot;chatbox__description--header&quot;&gt;Hi. My name is Sam. How can I help you?&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&quot;chatbox__messages&quot;&gt;
                &lt;div&gt;&lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&quot;chatbox__footer&quot;&gt;
                &lt;input type=&quot;text&quot; placeholder=&quot;Write a message...&quot;&gt;
                &lt;button class=&quot;chatbox__send--footer send__button&quot;&gt;Send&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;chatbox__button&quot;&gt;
            &lt;button&gt;&lt;img src=&quot;{{ url_for(&#39;static&#39;, filename=&#39;images/chatbox-icon.svg&#39;) }}&quot; /&gt;&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;script&gt;
    var $SCRIPT_ROOT = &quot;{{ request.script_root|tojson }}&quot;;
&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;{{ url_for(&#39;static&#39;, filename=&#39;app.js&#39;) }}&quot;&gt;&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
and here is my css code base for style.css:

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: &#39;Nunito&#39;, sans-serif;
    font-weight: 400;
    font-size: 100%;
    background: #F1F1F1;
}

*, html {
    --primaryGradient: linear-gradient(93.12deg, #581B98 0.52%, #9C1DE7 100%);
    --secondaryGradient: linear-gradient(268.91deg, #581B98 -2.14%, #9C1DE7 99.69%);
    --primaryBoxShadow: 0px 10px 15px rgba(0, 0, 0, 0.1);
    --secondaryBoxShadow: 0px -10px 15px rgba(0, 0, 0, 0.1);
    --primary: #581B98;
}

/* CHATBOX
=============== */
.chatbox {
    position: absolute;
    bottom: 30px;
    right: 30px;
}

/* CONTENT IS CLOSE */
.chatbox__support {
    display: flex;
    flex-direction: column;
    background: #eee;
    width: 300px;
    height: 350px;
    z-index: -123456;
    opacity: 0;
    transition: all .5s ease-in-out;
}

/* CONTENT ISOPEN */
.chatbox--active {
    transform: translateY(-40px);
    z-index: 123456;
    opacity: 1;

}

/* BUTTON */
.chatbox__button {
    text-align: right;
}

.send__button {
    padding: 6px;
    background: transparent;
    border: none;
    outline: none;
    cursor: pointer;
}


/* HEADER */
.chatbox__header {
    position: sticky;
    top: 0;
    background: orange;
}

/* MESSAGES */
.chatbox__messages {
    margin-top: auto;
    display: flex;
    overflow-y: scroll;
    flex-direction: column-reverse;
}

.messages__item {
    background: orange;
    max-width: 60.6%;
    width: fit-content;
}

.messages__item--operator {
    margin-left: auto;
}

.messages__item--visitor {
    margin-right: auto;
}

/* FOOTER */
.chatbox__footer {
    position: sticky;
    bottom: 0;
}

.chatbox__support {
    background: #f9f9f9;
    height: 450px;
    width: 350px;
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
}

/* HEADER */
.chatbox__header {
    background: var(--primaryGradient);
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 15px 20px;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    box-shadow: var(--primaryBoxShadow);
}

.chatbox__image--header {
    margin-right: 10px;
}

.chatbox__heading--header {
    font-size: 1.2rem;
    color: white;
}

.chatbox__description--header {
    font-size: .9rem;
    color: white;
}

/* Messages */
.chatbox__messages {
    padding: 0 20px;
}

.messages__item {
    margin-top: 10px;
    background: #E0E0E0;
    padding: 8px 12px;
    max-width: 70%;
}

.messages__item--visitor,
.messages__item--typing {
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
}

.messages__item--operator {
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    border-bottom-left-radius: 20px;
    background: var(--primary);
    color: white;
}

/* FOOTER */
.chatbox__footer {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 20px 20px;
    background: var(--secondaryGradient);
    box-shadow: var(--secondaryBoxShadow);
    border-bottom-right-radius: 10px;
    border-bottom-left-radius: 10px;
    margin-top: 20px;
}

.chatbox__footer input {
    width: 80%;
    border: none;
    padding: 10px 10px;
    border-radius: 30px;
    text-align: left;
}

.chatbox__send--footer {
    color: white;
}

.chatbox__button button,
.chatbox__button button:focus,
.chatbox__button button:visited {
    padding: 10px;
    background: white;
    border: none;
    outline: none;
    border-top-left-radius: 50px;
    border-top-right-radius: 50px;
    border-bottom-left-radius: 50px;
    box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.1);
    cursor: pointer;
}

I would appreciate any insights or suggestions on why the chatbot pop-up is not appearing when the button is clicked. Thank you!

答案1

得分: 0

在display()方法内部,

尝试将A更改为B

A

const chatBox = document.querySelector(".chatbox");

B

const chatBox = document.querySelector(".chatbox__support");
英文:

Within display() method,

try changing A to B

A

const chatBox = document.querySelector(&quot;.chatbox&quot;); 

B

const chatBox = document.querySelector(&quot;.chatbox__support&quot;); 

huangapple
  • 本文由 发表于 2023年6月26日 08:38:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定