- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用自定义菜单栏功能在 Google 表格中打开一个包含表单的侧边栏。
我想要<textarea>
每当侧边栏打开时就获得焦点(以便光标位于其中,并且任何键入都会立即添加到其中)。但使用focus()
不管用。 (将样式应用于它正在工作,如下一行所示。)
请问我怎样才能做到这一点?非常感谢您的帮助。
注意:它无法在 Chrome (Win 10) 中运行,但可以在 Firefox 中运行。
function abc() {
var html =
'<form id="form">' +
'<textarea id="title"> </textarea> '+
'</form> <button>Submit</button> '+
'<script>'+
' document.getElementById("title").focus(); '+ // << NOT WORKING
' document.getElementById("title").style.outline ="solid 2px red"' + // << WORKING
' </script>'+
'';
var a = HtmlService.createHtmlOutput(html)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('MY-FORM')
SpreadsheetApp.getUi().showSidebar(a);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('My Functions')
.addItem('Start', 'abc')
.addSeparator()
.addToUi();
}
最佳答案
您必须等待页面加载。
试试这个:
function abc() {
var html ='<form><input type="text" id="title" name="title" /><br /><input type="button" value="Submit" onclick="google.script.run.processForm(this.parentNode);"/></form>';
html+='<script>window.onload=function(){document.getElementById("title").focus();document.getElementById("title").style.outline="solid 2px red";}</script>';
var ui=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showSidebar(ui);
}
function processForm(obj) {
console.log(JSON.stringify(obj));
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
sh.getRange('A1').setValue(obj.title);
}
您还可以使用文本区域:
function abc() {
var html ='<form><textarea rows="1" cols="15" id="title" name="title"></textarea><br /><input type="button" value="Submit" onclick="google.script.run.processForm(this.parentNode);"/></form>';
html+='<script>window.onload=function(){document.getElementById("title").focus();document.getElementById("title").style.outline="solid 2px red";}</script>';
var ui=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showSidebar(ui);
}
关于javascript - focus() 在 Chrome 中的 getUi().showSidebar html 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60269854/
这个问题在这里已经有了答案: "SpreadsheetApp.getUi() cannot be called from this context" (4 个答案) Getting user inp
在Google表格电子表格中,我想显示一个用HTML创建的模式对话框,然后运行一个函数,然后自动关闭该HTML提示。 该对话框将一直保留,直到函数执行完毕,然后自动消失。 此过程必须每3小时重复一次,
我正在用 Java 开发外观。当我将其应用到我的程序时,我收到此异常/错误: UIDefaults.getUI() failed: no ComponentUI class for: javax.sw
我有一个基本问题。我正在使用 Vaadin 8。 当我使用时: Page.getCurrent().setTitle(IStringConstants.HOMEPAGE_LABEL); 这可以正常工作
试图在用户单击菜单项时添加确认对话框,我几乎完全按照这个 Google 页面进行操作:https://developers.google.com/apps-script/guides/dialogs#
我一直在尝试编写一个脚本来显示一个提示框,该提示框将接受用户输入以供我打开网站。 我正在尝试以下代码 var ui = SpreadsheetApp.getUi(); var response = u
function createAndSendDocument() { // Create a new document with the title 'Hello World' var ui = Do
我正在使用自定义菜单栏功能在 Google 表格中打开一个包含表单的侧边栏。 我想要每当侧边栏打开时就获得焦点(以便光标位于其中,并且任何键入都会立即添加到其中)。但使用focus()不管用。 (将样
我找到了this project它在独立运行中工作得很好。但是,当我尝试将其添加到另一个项目中的 JPanel 时(已经在完全相同的项目中执行此操作,但使用我自己的 JFrame 并且工作正常),则会
正如标题所示,我正在尝试从脚本获取 Google 表单的 UI,以便我可以向用户显示一个包含一些信息的警报框。 这是函数的相关部分: function Foobar(){ ... va
我是一名优秀的程序员,十分优秀!