问题描述
我正在创建一个非常基本的脚本,以协助我完成一个Google电子表格.
I'm creating an extremely basic script to assist me in one of my google spreadsheets.
我已经成功显示了一个带有一些按钮(功能)的侧边栏.但是,每当我单击其中一个按钮时,它也会打开一个新标签,其URL类似于:" https://n-lx3mdv5ls3mdgglsq226llilxd2m4owxy72y3fy-1lu-script.googleusercontent.com/userCodeAppPanel ?"
I've successfully got a sidebar showing, with a few buttons (which function). However, whenever I click on one of those buttons, it also opens up a new tab, with a URL something like: "https://n-lx3mdv5ls3mdgglsq226llilxd2m4owxy72y3fy-1lu-script.googleusercontent.com/userCodeAppPanel?"
即使按钮没有任何功能,这种情况仍然会发生.
Even if the button is left without any functionality, this still occurs.
这是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>
如何阻止每次打开新标签页?
How can I stop this new tab from opening every time?
推荐答案
问题:
-
<button>
类型(如果未指定)默认为type=submit
.因此,该表单会自动将数据提交到服务器.这在iframe中不是可取的. <button>
type, if not specified defaults totype=submit
. So, The form automatically submits the data to the server. This is not preferable in a iframe.- 将按钮类型明确指定为按钮. >
- 使用
event.preventDefault()
阻止自动提交表单. - Explicitly specify button type as button.
<button type='button'
> - Use
event.preventDefault()
to prevent automatic form submission. - Button#Default
- Sample Form
Issue:
这篇关于Google脚本-边栏按钮不断打开新标签页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!