Link 通过 href 到 JavaScript 函数

Link to a JavaScript function via href

我正在努力 link 使用 Google Apps 脚本 HTML link <a> 和 JavaScript 函数。我尝试了以下操作,但给我留下了这条错误消息:

"Unsafe JavaScript attempt to initiate navigation for frame with origin 'https://script.google.com' from frame with URL 'different url'. The frame attempting navigation must be same-origin with the target if navigating to a javascript: url"

我的代码:

var link = document.createElement("a");
link.setAttribute("href", "javascript:test()");
link.textContent = "Click";

//----------------------------------------------
function test() {
    console.log("Test");
}

代码放在 HTML 文件中:

<script>
</script>

一切都在 Google Apps 脚本的 IDE 中编码。

不要使用 href。将 onclick 事件用于 JavaScript 函数。

改为使用 onclick javascript 函数:

<a onclick="myFunction()">Click me</a>

https://www.w3schools.com/jsref/event_onclick.asp

href是一个用来定义link对象的属性。用于点击触发事件的正确属性是 onclick.

请参阅 href and onclick

的 W3C 文档