如果在调用函数时提供的参数包含空格,则不调用函数
If the parameter supplied while calling a function contains space, function is not called
我使用的是last.fm api。我有一个JavaScript AJAX函数,它是这样的,
function showart(art) {
if (art=="") {
document.getElementById("info").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("info").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","5.php?a="+art,true);
xmlhttp.send();
}
这是我称之为的PHP代码
for($i=1;$i<=10;$i++) {
echo '<table><td width="36px">';
echo '<img src="'.$info->topartists->artist[$i-1]->image.'"></td>';
echo '<td><a class="artist" href=# onclick=showart('''.$info->topartists->artist[$i-1]->name.''')>'.$info->topartists->artist[$i-1]->name.'</a></td></table>';
}
每当$info->topartists->artist[$i-1]->name
在艺术家的名字中有一个空格时,例如"Mobb Deep",函数就不会被调用,如果艺术家的名字是"Adele",函数就会被调用。如何解决此问题?
您必须对参数值进行编码:
xmlhttp.open("GET","5.php?a=" + encodeURIComponent(art), true);
这将对空间进行正确编码,服务器将对其进行解码
edit创建<a>
标记的代码没有将"onclick"属性值放在引号中,因此当解析HTML时,事件处理程序代码最终会被破坏。你应该引用代码:
echo '<td><a class="artist" href=# onclick="showart('''.$info->topartists->artist[$i-1]->name.''')">'.$info->topartists->artist[$i-1]->name.'</a></td></table>';
注意在showart()
调用周围添加的"
引号。
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量