Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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()在chrome中不起作用,字符串中有空格_Javascript - Fatal编程技术网

javascript函数indexOf()在chrome中不起作用,字符串中有空格

javascript函数indexOf()在chrome中不起作用,字符串中有空格,javascript,Javascript,我想在锚标记中搜索字符串,当我只搜索没有空格的“Happy”时,它工作正常,但是当我搜索像“Happy”这样的空格时,它不工作 下面是代码示例: <html> <body> <a style="color:#555555" href="Happy coding!!">test</a> <br/> <script type="text/javascript"> var hrefTags = document.getEl

我想在锚标记中搜索字符串,当我只搜索没有空格的“Happy”时,它工作正常,但是当我搜索像“Happy”这样的空格时,它不工作

下面是代码示例:

<html>
<body>

 <a style="color:#555555" href="Happy coding!!">test</a>
<br/>

<script type="text/javascript">

var hrefTags = document.getElementsByTagName("a");
var bDayId = ""; 
var stringToSearch = "Happy coding!!";
    for(var hrefIndex=0; hrefIndex < hrefTags.length; hrefIndex ++){
bDayId = hrefTags.item(hrefIndex).href.toString(); 
document.write(bDayId+"<br/>");
document.write( bDayId.indexOf("Happy "));  
     }  

</script>

</body>
</html>


var hrefTags=document.getElementsByTagName(“a”); var bDayId=“”; var stringToSearch=“快乐编码!!”; 对于(var hrefIndex=0;hrefIndex”); 书面文件(b日期id.indexOf(“快乐”); }
当我在Firefox中这样做时,我从
toString()中得到的字符串是:

file:///home/pax/Happy%20coding!!
如果这就是你所看到的,那么你当然无法在那里找到“快乐”
。当我将搜索语句更改为:

document.write( bDayId.indexOf("Happy%20"));
那么它工作得很好

也许您需要考虑url对搜索字符串的编码方式,类似于浏览器在
toString()
中的编码方式:


当然,这是假设我的编码是正确的。您没有提供HTML的实际输出,因此有点难以判断


将其作为问题的编辑发布是值得的。

使用正则表达式匹配字符串 查看以下URL将有所帮助:

因为href是URL,所以URL编码,所以空格变成
'%20'
。 带有空格的Href是无效的Href,因此如果要在Href属性中存储某些信息,请使用另一个属性, 或者,如果要读取href属性并将其与空格进行字符串比较,请在正确的url解码后进行比较

var hrefTags=document.getElementsByTagName(“a”);
var bDayId=“”;
var stringToSearch=“快乐编码!!”;
对于(var hrefIndex=0;hrefIndex”);
书面文件(b日期id.indexOf(“快乐”);
}  

从link href属性检索时,空间将被编码为“%20”。 请尝试以下内容:

bDayId = decodeURIComponent(hrefTags.item(hrefIndex).href.toString());
var hrefTags = document.getElementsByTagName("a");
var bDayId = ""; 
var stringToSearch = "Happy coding!!";
for(var hrefIndex=0; hrefIndex < hrefTags.length; hrefIndex ++){
  bDayId = decodeURIComponent(hrefTags.item(hrefIndex).href);      
  document.write(bDayId);    
  document.write(bDayId+"<br/>");
  document.write( bDayId.indexOf("Happy "));  
}  
bDayId = decodeURIComponent(hrefTags.item(hrefIndex).href.toString());