简体   繁体   English

如何实现.包含在JavaScript

[英]How to implement .includes in JavaScript

I'm developing a very simple chatbot using JS and HTML, and I have the following code which outputs an "answer" as a reply to what the user has asked.我正在使用 JS 和 HTML 开发一个非常简单的聊天机器人,我有以下代码输出“答案”作为对用户所问内容的答复。 This works fine but when I try to implement .includes to check if a certain phrase has been asked it no longer outputs anything.这很好用,但是当我尝试实现.includes以检查是否已询问某个短语时,它不再输出任何内容。 For example, if the user asks "can you help" it should output the same answer as "help" since it contains the "help" keyword.例如,如果用户问“can you help”,它应该 output 得到与“help”相同的答案,因为它包含“help”关键字。 Without .includes the code works fine.没有.includes代码工作正常。

function talk() {

  var know = {
    "Hey": "Hello!",
    "Help": `You can find help by clicking <a href='https://addressname.com'target="_blank">here</a>`
  };

  var user = document.getElementById('userBox').value;
  
  document.getElementById('chatLog').innerHTML = user + "<br>";
  
  if (user in know) {
    document.getElementById.includes('chatLog').innerHTML = know[user] + "<br>";
  } else {
    document.getElementById('chatLog').innerHTML = "I do not understand.";
  }

}

I guess you need to change below line我想你需要改变下面的行

document.getElementById.includes('chatLog').innerHTML

as作为

document.getElementById('chatLog').innerHTML.includes(user)

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. includes() 方法确定数组是否在其条目中包含某个值,并根据需要返回 true 或 false。

Hope this helps:希望这可以帮助:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Update:更新:

    var know = {
  "Hey": "Hello!",
  "Help": `You can find help by clicking <a href='https://addressname.com' target="_blank">here</a>`
};

function goo() {
  var userBox = document.getElementById('userBox');
  var userInput = userBox.value;
  var chatLog = document.getElementById('chatLog');
  var chatLogContent = "";

  if (!userInput) {
    chatLogContent = 'Please enter some value'
  }

  var hasKeyword = false;

  for (var key in know) {
    if (userInput.toLowerCase().includes(key.toLowerCase())) {
      hasKeyword = true;
      break;
    }
  }

  if (hasKeyword) {
    chatLogContent += know[key] + "<br>"; //or use know.key
  } else {
    chatLogContent += "I do not understand.<br>";
  }

  var client = document.createElement('div');
  client.setAttribute('class', 'client');
  client.innerHTML += userInput + "<br>";

  chatLog.appendChild(client);

  var server = document.createElement('div');
  server.setAttribute('class', 'server');
  server.innerHTML = chatLogContent;

  chatLog.appendChild(server);

}

Update更新
This should be helpful:这应该有帮助:
https://jsfiddle.net/smileyface/948bg03n/1/ https://jsfiddle.net/smileyface/948bg03n/1/
Well, I am not good at css. But it works perfectly.嗯,我不擅长 css。但它工作得很好。 Scroll through each time you click on button.每次单击按钮时滚动浏览。
Try here - Localhost chat box is here : https://jsfiddle.net/smileyface/948bg03n/1/show在这里试试 - Localhost 聊天框在这里https://jsfiddle.net/smileyface/948bg03n/1/show

Update更新
As you asked in comment,正如您在评论中所问,
This will only show current comments这只会显示当前评论

if(hasKeyword){
    chatLogContent = know[key] + "<br>"; //or use know.key
}else{
    chatLogContent = "I do not understand.<br>";
}

chatLog.innerHTML = chatLogContent;

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. includes() 方法确定数组是否在其条目中包含某个值,并根据需要返回 true 或 false。

Also getElementById is a function which expects the id of an element as a parameter. getElementById 也是一个 function ,它需要一个元素的 id 作为参数。

If you want to change the inner HTML of an element having id chatLog then you have to do it the following way.如果要更改具有 id chatLog的元素的内部 HTML,则必须按以下方式进行。

if (user in know) {
    document.getElementById('chatLog').innerHTML = know[user] + "<br>";
  }

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

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