jquery match:如何查找数组中是否存在关键字?
如何查找使用split()拆分的数组中是否存在关键字?
// Get the object infomation. var keyword = 'story'; var path = $(this).find('a').attr('href'); var array_url = path.split('/'); if(keyword == array_url) alert('match!'); // does it work like this??
标签的url路径是这样的 – www.mysite.com/story/article-1
谢谢。
编辑:
if ($.inArray(keyword, array_url)) alert('match!');
这将提醒匹配数组中是否包含关键字。
if(keyword == array_url) alert('match!');
//这样工作吗?
好吧,是吗? ;)
jQuery具有搜索数组的便利function 。
if ( $.inArray(keyword, array_url) > -1 ) alert('match!)';
或者,在您的情况下,您可以使用常规的JavaScript字符串操作:
if ( $(this).find('a').attr('href').indexOf(keyword) > -1 ) alert('match!)';
如果未找到匹配,则jQuery.inArray返回-1。
你应该做
if ($.inArray(keyword, array_url) != -1) alert('match!');
见http://api.jquery.com/jQuery.inArray/
if ($.inArray(keyword, array_url) > -1) alert('match!');