簡體   English   中英

如何將javascript值傳遞給html href屬性?

[英]How to pass javascript value to html href attribute?

我想在HTML正文內的href屬性中傳遞finalName的值。 我該怎么辦?

<html>
<script type="text/javascript" language="Javascript">
        var url = window.location.search;
        var val = getParameter('n');
        var finalName = "";
        if( val == false ) {
            var a= prompt("Enter your name & see the magic", "");
            if(a){
                window.location.href = '?n='+a;
                document.getElementById("demo2").innerHTML = a ;
                finalName = a;
            }
        }
        else {
            document.getElementById('demo2').innerHTML = val;
        }
</script>

<body>
<a class="footerbtn" href="whatsapp://send?text= send you special surprise message%0A%0ATouch this blue line %0Aenter your name %0A http://magic.pagalworld.buzz/?n=" 
  onclick="location.href=this.href+finalName;return false;"><img width="25px" height="25px" src="wp.png"/><b style="font-size: 15px;"> Click Here to Share on Whatsapp</b> <img width="25px" height="25px" src="wp.png"/></a>
</body>
</html>

您可以使用window.finalName使變量可訪問。 例如:

 <script type="text/javascript" language="Javascript"> var url = window.location.search; var val = getParameter('n'); window.finalName = ""; if( val == false ) { var a= prompt("Enter your name & see the magic", ""); if(a){ window.location.href = '?n='+a; document.getElementById("demo2").innerHTML = a ; window.finalName = a; } } else { document.getElementById('demo2').innerHTML = val; } </script> 
 <html> <body> <a class="footerbtn" href="whatsapp://send?text= send you special surprise message%0A%0ATouch this blue line %0Aenter your name %0A http://magic.pagalworld.buzz/?n=" onclick="location.href=this.href+finalName;return false;"><img width="25px" height="25px" src="wp.png"/><b style="font-size: 15px;"> Click Here to Share on Whatsapp</b> <img width="25px" height="25px" src="wp.png"/></a> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM