You really don't need jQuery for this.
(您真的不需要jQuery。)
var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles") > -1);
Hint : indexOf returns a number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs
(提示 :indexOf返回一个数字,表示指定的搜索值首次出现的位置;如果从未出现,则返回-1)
or
(要么)
function arrayContains(needle, arrhaystack)
{
return (arrhaystack.indexOf(needle) > -1);
}
It's worth noting that array.indexOf(..)
is not supported in IE < 9 , but jQuery's indexOf(...)
function will work even for those older versions.
(值得注意的是IE <9不支持 array.indexOf(..)
,但jQuery的indexOf(...)
函数即使在那些较旧的版本中也可以使用。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…