将变量传递给按钮

时间:2016-03-26 20:48:28

标签: javascript html google-apps-script

我在服务器端获取一个URL。

<script>
    function onSuccess(ID_FOLDER) {
    var div = document.getElementById('output');
    var FolderPathURL = "https://drive.google.com/drive/folders/"+ID_FOLDER+"";
    div.innerHTML = '"+FolderPathURL+"'
    }

    google.script.run.withSuccessHandler(onSuccess).getFolderIds();

</script>

变量FolderPathURL现在位于onSuccess函数中。我需要将它作为href(链接)应用于HTML视图中的按钮。因此,当用户点击按钮时,他们会转到“+ FolderPathURL +”中提供的链接。这对我来说应该是一个简单的任务,但我现在已经被困住了6个小时......:)

  <button class="action">View Folder</button>

我到目前为止尝试了以下但没有成功,这取代了我需要的蓝色css按钮:

div.innerHTML = "<form><button formaction='"+FolderPathURL+"'>View Folder</button></form>"

然后在HTML中:

  <div id="output"></div>

2 个答案:

答案 0 :(得分:1)

  

我需要将它作为href(链接)应用于HTML视图中的按钮。因此,当用户点击按钮时,他们会转到&#34; + FolderPathURL +&#34;中提供的链接。

试试这个,在按钮

中放置一个锚标签
div.innerHTML = "<button formaction='"+FolderPathURL+"'>
                  <a href='"+ FolderPathURL +"' class='anchorButton'>View Folder</a></button>";

使用此CSS规则

.anchorButton{
 text-decoration:none;
 color:white;
 background-color: Blue;
}

答案 1 :(得分:-1)

试试这个:

var FolderPathURL = "/this/that/this/that.html";


document.getElementById("output").innerHTML = "<form><button formaction='"+FolderPathURL+"'>View Folder</button></form>";
<div id="output"></div>

我认为你的问题只与你选择div的方式有关!

您正在使用:

div.innerHTML = blahblah;

在你的情况下,我认为你需要使用它:

document.getElementById("output").innerHTML = blahblah;

希望这有帮助!

祝你的项目好运!

相关问题