将动态生成的值从javascript函数发送到另一个javascript函数

时间:2011-05-10 12:26:17

标签: javascript function dynamic

我的网站项目遇到了另一个困难。好的,这是我的问题......

    <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');

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

2 个答案:

答案 0 :(得分:1)

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

使用转义单引号\'

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

答案 1 :(得分:1)

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