我无法找到一种方法来搜索字母/字符串内的子字符串。目标是在找到字符串时创建警报。由于检查一个字/字母是否包含在一个字符串与JavaScript
https://jsfiddle.net/1rawcotx/2/
function validate() {
if (string.indexOf('worlds') > -1) {
alert("word found");
}
}
<div id="string">
worlds and some other text
</div>
<button id="button" onclick="validate()">click me</button>
* “字符串是未定义的变量” *其实,这是不是在OP的代码;这是一个自动的全球(不寒而栗)。 –