我想将 getElementByID 方法与 String Includes () 方法结合使用。我怎样才能做到这一点?
I want to use the getElementByID method with the String Includes () method. How can I do that?
在我正在制作的网站上,我有这个脚本来检查用户是否输入了某个关键短语,如果已经提交了关键短语,则调用一个函数。
function onClick() {
if (document.getElementById("user_input").value === "I hate the EU!")
{
antiEuropeExample();
}
基本上如果用户输入恰好是“我讨厌欧盟!” antiEuropeExample();函数调用。我想知道是否可以让函数查找关键字,所以只要关键字存在,函数就会调用。因此,不必完全输入“我讨厌欧盟!”您可以输入“The EU sucks”,该函数仍会调用。
我在网上找到了这个方法:
<script>
function myFunction() {
var str = "Hello world, welcome to the universe.";
var n = str.includes("world");
document.getElementById("demo").innerHTML = n;
}
</script>
这看起来很有希望,但我不知道如何将我已经编写的脚本与 str.includes 方法结合起来;我不知道这是否可能,或者是否有更简单的方法。似乎在此示例中,您必须自己在代码中定义字符串,这对我没有帮助,因为用户可以在网站中键入任何内容(或者我可能误解了代码,我不知道!)我目前只使用 JavaScript,而不是 JQuery,如果有帮助的话。
非常感谢您的帮助!
在这个 onClick 函数示例中,您将完全按照您引用的示例执行关键字检查。
if 语句块中的示例如下:
if(document.getElementById("user_input").value.includes("I hate the EU!"))
{
antiEuropeExample();
}
此条件 returns 布尔值 true 或 false 取决于字符串是否包含您作为参数传入的短语。您必须在 if 语句中包含 && 以指定块执行的最低条件,例如该字符串必须包含 xyz、xyz 和 xyz。
你想要这样的东西吗?
function onClick() {
if (document.getElementById("user_input").value.includes("EU"))
{
//antiEuropeExample();
alert("ANTI EU!!!");
}
}
<input id="user_input">
<button onclick="onClick()">CLick :)</button>
只有一些信息:.includes() 是区分大小写的,所以上面的例子只有当你写 EU 时才有效,如果你写 eu 则不会.
如果你想让它不区分大小写,请尝试将 .toUpperCase() 放在 .includes() 之前:
function onClick() {
if (document.getElementById("user_input").value.toUpperCase().includes("EU"))
{
//antiEuropeExample();
alert("ANTI EU!!!");
}
}
<input id="user_input">
<button onclick="onClick()">CLick :)</button>
在我正在制作的网站上,我有这个脚本来检查用户是否输入了某个关键短语,如果已经提交了关键短语,则调用一个函数。
function onClick() {
if (document.getElementById("user_input").value === "I hate the EU!")
{
antiEuropeExample();
}
基本上如果用户输入恰好是“我讨厌欧盟!” antiEuropeExample();函数调用。我想知道是否可以让函数查找关键字,所以只要关键字存在,函数就会调用。因此,不必完全输入“我讨厌欧盟!”您可以输入“The EU sucks”,该函数仍会调用。
我在网上找到了这个方法:
<script>
function myFunction() {
var str = "Hello world, welcome to the universe.";
var n = str.includes("world");
document.getElementById("demo").innerHTML = n;
}
</script>
这看起来很有希望,但我不知道如何将我已经编写的脚本与 str.includes 方法结合起来;我不知道这是否可能,或者是否有更简单的方法。似乎在此示例中,您必须自己在代码中定义字符串,这对我没有帮助,因为用户可以在网站中键入任何内容(或者我可能误解了代码,我不知道!)我目前只使用 JavaScript,而不是 JQuery,如果有帮助的话。
非常感谢您的帮助!
在这个 onClick 函数示例中,您将完全按照您引用的示例执行关键字检查。
if 语句块中的示例如下:
if(document.getElementById("user_input").value.includes("I hate the EU!"))
{
antiEuropeExample();
}
此条件 returns 布尔值 true 或 false 取决于字符串是否包含您作为参数传入的短语。您必须在 if 语句中包含 && 以指定块执行的最低条件,例如该字符串必须包含 xyz、xyz 和 xyz。
你想要这样的东西吗?
function onClick() {
if (document.getElementById("user_input").value.includes("EU"))
{
//antiEuropeExample();
alert("ANTI EU!!!");
}
}
<input id="user_input">
<button onclick="onClick()">CLick :)</button>
只有一些信息:.includes() 是区分大小写的,所以上面的例子只有当你写 EU 时才有效,如果你写 eu 则不会.
如果你想让它不区分大小写,请尝试将 .toUpperCase() 放在 .includes() 之前:
function onClick() {
if (document.getElementById("user_input").value.toUpperCase().includes("EU"))
{
//antiEuropeExample();
alert("ANTI EU!!!");
}
}
<input id="user_input">
<button onclick="onClick()">CLick :)</button>