Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中搜索具有indexOf的字符串_Javascript_String_Url_Search_Indexof - Fatal编程技术网

在Javascript中搜索具有indexOf的字符串

在Javascript中搜索具有indexOf的字符串,javascript,string,url,search,indexof,Javascript,String,Url,Search,Indexof,我在正确理解indexOf功能方面仍然有问题,或者还有一些我看不到的东西 我目前正在使用此for循环搜索URL中的文本字符串/段: for(var i = 0; i < links.length; i++) { element = links[i]; if(element.href.indexOf(segment) !== -1) { return true; } 例如,如果URL为:http://www.anywebsite.com/thi

我在正确理解indexOf功能方面仍然有问题,或者还有一些我看不到的东西

我目前正在使用此for循环搜索URL中的文本字符串/段:

for(var i = 0; i < links.length; i++) {
    element = links[i];
    if(element.href.indexOf(segment) !== -1) {
        return true; 
      }
例如,如果URL为:http://www.anywebsite.com/this_is_my_text?some¶meters 而segment=这是我的文本它工作得很好

但是,如果URL如下所示:http://www.anywebsite.com/this_is_my_textand segment=this_is_my_text找不到段/字符串,此for循环周围的函数返回false和not true

我做错了什么?我真的看不见

提前多谢

我在这里测试过::


我刚刚在if语句中添加了“segment”,现在它在这两种情况下都起作用了,在此编辑之前,这两种情况都不起作用。

解决方案:代码是正确的,来自网站的数据接收速度太慢。

我也是。您可以制作一个JSFIDLE或一个复制问题的SO代码片段吗?我对它进行了如下测试:alerthttp://www.anywebsite.com/this_is_my_text.indexOfthis_is_my_text; 它发出了26的警报,26仍然大于-1,所以这在我这边有效。如果可能,记录所有值,并查看是否未找到任何值。并尝试element.getAttributehref以确保安全。是否确定元素中未定义href?因为你所拥有的应该有用。实际上,我需要href,因为URL将通过jQuery:links=$来自网站;我可以在URL中找到所有结果,如http://www.anywebsite.com/this_is_my_text?some¶meters 但不是像这样的URLhttp://www.anywebsite.com/this_is_my_text -真的weird@xqz313我正在测试你自己对这两个URL的处理,我发现两次都是这样。在我的例子中没有太多的效果:实际上我需要href,因为URL会出现通过jQuery从一个网站获取:links=$。列表\u行\u链接;我可以在URL中找到所有结果,如http://www.anywebsite.com/this_is_my_text?some¶meters 但不是像这样的URLhttp://www.anywebsite.com/this_is_my_text -真奇怪
var u1 = 'http://www.anywebsite.com/this_is_my_text?some&parameters and segment = "this_is_my_text"';
var u2 = 'http://www.anywebsite.com/this_is_my_textand segment = "this_is_my_text"';

    if(u2.indexOf('segment') !== -1) {
        return alert('found'); 
      }