2011-05-10 77 views
0

我的网站项目存在另一个问题。确定这里是我的问题...
从javascript函数向另一个javascript函数发送动态生成的值

<script> 
function getTopArtist(){ 
    if (window.XMLHttpRequest){ 
     topartist = new XMLHttpRequest(); 
    } 
    else{ 
     topartist = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    topartist.onreadystatechange=function(){ 
     try{ 
      if (topartist.readyState==4 && topartist.status==200){ 
       var ArtistDetails = topartist.responseXML.documentElement.getElementsByTagName("artist"); 

       for(i=0;i<=2;i++){ 
        myartistname = ArtistDetails[i].getElementsByTagName('name')[0].firstChild.nodeValue; 
        alert(myartistname) 
        document.getElementById('topartistdiv').innerHTML+='<a href="javascript:getAlbums(this is the proble here);">Albums</a>'; 
      } 
     } 
     catch(er){ 
      alert("Oops something went wrong!"); 
     } 
    } 
    topartist.open("GET","http://localhost/test/topartist.php",true); 
    topartist.send(null); 
} 
</script> 

我的问题是在第17行,当我试图把艺术家姓名括号里面的话,我可以将它们发送给另一个函数。所以我们假设它提醒Beyonce我希望链接是这样的。

javascript:getAlbums('Beyonce'); 

我想它与特殊字符有关,但我无法弄清楚。任何帮助将不胜感激。

回答

1

您需要引用字符串,并且您已经使用了'作为JavaScript字符串,"作为HTML属性字符串。

使用转义单引号,\'

'<a href="javascript:getAlbums(\'no problemo\');">Albums</a>' 
+0

会给我一个意外的标记catch错误的铬。 – sam 2011-05-10 12:32:06

1
document.getElementById('topartistdiv').innerHTML += '<a href="javascript:getAlbums(\'Beyonce\');">Albums</a>'; 
相关问题