2011-05-10 43 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>'; 
相關問題