我有一個簡單的 WebApp。
我在另一個檔案中撰寫了腳本(不是直接在 Index.html 中)
我成功地將Index.html中的onClick 屬性設定為ButtonA。
并嘗試從JavaScript.html檔案將另一個 onClick 屬性設定為ButtonB 。但它不起作用:(
出了什么問題或如何實作這一目標..?
網路應用鏈接
應用腳本鏈接
下面是 Code.gs 檔案
function doGet() {
var template = HtmlService.createTemplateFromFile('Index.html') ;
var TodaysUrl = "https://google.com" ;
template.TodaysUrl = TodaysUrl;
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent() ;
}
下面是 Index.html 檔案
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
.button {
display : block ;
text-align : center ;
margin : 25px auto ;
padding : 10px
}
</style>
</head>
<body>
<button class="button" id="btn-a" >ButtonA Link from Index.html</button>
<button class="button" id="btn-b" >ButtonB Link from JavaScript.html</button>
<script>
document.getElementById("btn-a").onclick = function () { window.open("<?= TodaysUrl ?>" )} ;
</script>
<?!= include('JavaScript'); ?>
</body>
</html>
下面是 JavaScript.html 檔案
<script>
document.getElementById("btn-b").onclick = function () { window.open("<?= TodaysUrl ?>" )} ;
</script>
提前致謝
uj5u.com熱心網友回復:
您的第二個按鈕不起作用,因為第二次加載,并且不是由“評估”功能呈現。
doGet > 評估(定義 btnA) > 包含 JS 檔案(包含 btnB,因此不會渲染)
我的建議是使用加載 DOM 后呼叫的自定義函式來初始化按鈕。這是一個帶有 2 個不同 URL 的示例
代碼.gs
function doGet() {
var template = HtmlService.createTemplateFromFile('Index.html') ;
// var TodaysUrl = "https://google.com" ;
// template.TodaysUrl = TodaysUrl;
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent() ;
}
function initialize() {
var obj = {};
obj.url_a = "http://google.com";
obj.url_b = "http://amazon.com";
return obj;
}
索引.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
.button {
display : block ;
text-align : center ;
margin : 25px auto ;
padding : 10px
}
</style>
</head>
<body>
<button class="button" id="btn-a" >ButtonA</button>
<button class="button" id="btn-b" >ButtonB</button>
<?!= include('JavaScript'); ?>
</body>
</html>
Javascript.html
<script>
document.addEventListener("DOMContentLoaded", function() {
google.script.run.withSuccessHandler(return_initialize).initialize();
});
function return_initialize(obj) {
document.getElementById("btn-a").onclick = function () { window.open(obj.url_a)} ;
document.getElementById("btn-b").onclick = function () { window.open(obj.url_b)} ;
}
</script>
uj5u.com熱心網友回復:
您需要傳遞TodaysUrl給模板。
function doGet() {
var TodaysUrl = "https://google.com" ;
var template = HtmlService.createTemplateFromFile('Index') ;
template.TodaysUrl = TodaysUrl;
return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
將您的腳本包含在 html 的正文中,而不是來自另一個檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/511253.html
上一篇:tkinter,python:在Listbox之后按鈕未顯示,并且Listbox根本沒有回應
下一篇:XLS0504屬性“FontFamily”不支持“OnPlatform`1(T)”型別的值XamarinForms
