我正在创建一个非常基本的脚本来帮助我处理我的一个谷歌电子表格。
我已经成功地显示了一个侧边栏,其中有几个按钮(哪个功能)。但是,每当我单击其中一个按钮时,它还会打开一个新选项卡,其 URL 类似于:“https://n-lx3mdv5ls3mdgglsq226llilxd2m4owxy72y3fy-1lu-script.googleusercontent.com/userCodeAppPanel ?”
即使按钮没有任何功能,这种情况仍然会发生。
这是 html 的示例:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<!-- The CSS package above applies Google styling to buttons and other elements. -->
<style>
</style>
</head>
<body>
<div class="sidebar">
<form>
<div class="block" id="buttons">
<button id="unindent">Unindent</button>
<button id="indent">Indent</button>
<button id="asdgdgasg">Test</button>
</div>
</form>
</div>
</body>
</html>
我怎样才能阻止这个新标签每次都打开?
最佳答案
问题:
-
<button>
类型,如果未指定默认为type=submit
.所以,表单自动将数据提交给服务器。这在 iframe 中不可取。
解决方案:
- 将按钮类型明确指定为按钮。
<button type='button'
> - 使用
event.preventDefault()
防止自动提交表单。
引用资料:
关于javascript - Google Script - 侧边栏按钮不断打开一个新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53825069/