gpt4 book ai didi

javascript - 如何在用javascript写一个字后运行一个事件

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:52 25 4
gpt4 key购买 nike

你好,我试了很多都解决不了。任何人都可以向我解释一下我将如何在写入“in”之后驱动 JavaScript 事件。我的意思是我想在写入“in”后将 div display:none 制作成 display:block。这是一个自动建议问题,我附上了一个图像,模式就像那个 [word] [in] [word]。这将执行 onkeyup 事件。

enter image description here

最佳答案

JS 版本...

var myInput = document.getElementById('myInput');
var messageBox = document.getElementById('messageBox')

myInput.addEventListener("keyup", checkString);

function checkString() {
var str = myInput.value;
// notice whitespace either side of "in" to prevent false positives
if (str.indexOf(' in ') > -1) {
messageBox.style.display = "block";
} else {
messageBox.style.display = "none";
}
}
#messageBox {
display: none;
padding: 15px;
background: #f2f2f2;
border: 1px solid #e6e6e6;
margin-top: 15px;
}
<input id="myInput" placeholder="Type here">

<div id="messageBox">In was typed</div>

关于javascript - 如何在用javascript写一个字后运行一个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30595527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com