javascript 从 ListView 的 ItemInvoked 事件以编程方式设置输入框的焦点 [Windows 8 Metro HTML5]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10690909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically set focus of input box from ListView's ItemInvoked event [Windows 8 Metro HTML5]
提问by JT703
Edit:If you have not developed on Windows 8, do not try to answer this question. Do not vote on it. Do not even read it. This is not a web app or website and does not run in a browser. Please stop down-voting content you do not understand. This is a Windows 8 METRO HTML5/js application that runs in the Windows Runtime environment.
编辑:如果您没有在 Windows 8 上开发,请不要尝试回答这个问题。不要投票。甚至不要阅读它。这不是网络应用程序或网站,也不是在浏览器中运行。请停止对您不理解的内容投反对票。这是一个在 Windows 运行时环境中运行的 Windows 8 METRO HTML5/js 应用程序。
Original Question:
原问题:
I want the cursor to be "blinking" in the input box, ready to receive input. I'm using javascript to set the focus. This isn't working:
我希望光标在输入框中“闪烁”,准备接收输入。我正在使用 javascript 来设置焦点。这不起作用:
document.querySelector("#input-box").focus();
Anyone know why? Is the focus method not the correct one to use for this?
有谁知道为什么?焦点方法不是用于此的正确方法吗?
Thank you.
谢谢你。
Edit #2:So it definitely has something to do with the fact that I am trying to set focus to the input from a ListView's "itemInvoked" event. The event is firing properly, the element is accessible from the event handler, and the line has no errors on execution. I can set focus to my input tag from a standard button click event, but not from an ItemInvoked event. So the question is, why can't I set focus from within this event handler?
编辑#2:所以它肯定与我试图将焦点设置为来自 ListView 的“itemInvoked”事件的输入的事实有关。事件正确触发,元素可从事件处理程序访问,并且该行在执行时没有错误。我可以从标准按钮单击事件将焦点设置到我的输入标记,但不能从 ItemInvoked 事件中设置焦点。所以问题是,为什么我不能从这个事件处理程序中设置焦点?
采纳答案by ChristiaanV
I have created this little test project with only a textbox and in the onload set the focus in exactly the same way as you do. I tried it both on the emulator and on my local machine and both methods seem to work.
我创建了这个只有一个文本框的小测试项目,并在 onload 中以与您完全相同的方式设置焦点。我在模拟器和本地机器上都试过了,两种方法似乎都有效。
Could you try if this project is working for you on your machine? And can you give us some more insight on when you are trying to set the focus? Right now in your question there is not that much information about when it's happening.
你能试试这个项目在你的机器上是否适合你吗?当您尝试设置焦点时,您能否给我们一些更深入的了解?现在在你的问题中,关于它何时发生的信息并不多。
You can download the sample over here http://www.playingwith.net/Temp/TestFocusApplication.zip
您可以在此处下载示例http://www.playingwith.net/Temp/TestFocusApplication.zip
Update
更新
Okay, seem to found it, if you call the msSetImmediate function with the function that sets focus to the textbox it seems to work. I don't have an option to upload the test example, so below you find the code where I attach the ItemInvoked handler and call the setFocus function.
好的,似乎找到了,如果您使用将焦点设置到文本框的函数调用 msSetImmediate 函数,它似乎可以工作。我没有上传测试示例的选项,因此您可以在下面找到我附加 ItemInvoked 处理程序并调用 setFocus 函数的代码。
var listView = document.getElementById("basicListView");
listView.winControl.addEventListener("iteminvoked", function (evt) {
if (listView.winControl.selection.count() > 0) {
msSetImmediate(setFocus);
}
});
function setFocus() {
//Set focus on input textbox;
var input = document.querySelector("#input-box")
input.focus();
}
回答by Orry S
Which build are you on ? consumer preview or other dev previews ? I'm on DP6 (available to MS partners) and the JS standard stuff works.
你在哪个版本?消费者预览或其他开发预览?我在 DP6(可供 MS 合作伙伴使用)并且 JS 标准的东西有效。
In my default.html I have:
在我的 default.html 我有:
<input id="input1" type="text" value="one"/>
<input id="input2" type="text" value="two"/>
<input id="focus_btn" type="button" value="Set focus 1"/>
<input id="focus_btn2" type="button" value="Set focus 2"/>
And then in my default.js, in the app-launched-loaded boilerplate function I have:
然后在我的 default.js 中,在 app-launched-loaded 样板函数中,我有:
document.querySelector('#focus_btn').addEventListener('click', function () {
var input1 = document.querySelector('#input1').focus();
});
document.querySelector('#focus_btn2').addEventListener('click', function () {
var input1 = document.querySelector('#input2').focus();
});
The only reason I can think of it not working is that the element is accessed before it is ready. That or earlier build bugs. I have the solution rar'd in : http://www.mediafire.com/?ghz49gtfxlgr7enif you want to see.
我认为它不起作用的唯一原因是该元素在准备好之前被访问。那个或更早的构建错误。如果你想看,我有解决方案:http: //www.mediafire.com/?ghz49gtfxlgr7en 。
回答by Firdous Ahmad
Although the question is very old. But I struggled to find the solution for this.
虽然这个问题很老了。但我努力寻找解决方案。
First thing .focus() does not return any value it just set focus and undefined is returned.
首先 .focus() 不返回任何值,它只是设置焦点并返回 undefined 。
Also, when we set focus on some element make sure that element has the tabindex present, otherwise it would not focus (mainly in chrome).
此外,当我们将焦点设置在某个元素上时,请确保该元素存在 tabindex,否则它不会获得焦点(主要是在 chrome 中)。
The following query may be used:
可以使用以下查询:
element.querySelector('#label').setAttribute('tabindex','0');
element.querySelector('#label').focus();
回答by Steve Binder
Try using non-jquery regular javascript like
尝试使用非 jquery 常规 javascript,如
document.getElementById( 'myElementsID' ).focus()
document.getElementById( 'myElementsID' ).focus()
回答by BenM
Try this to see if it helps:
试试这个,看看它是否有帮助:
if(document.createEvent)
{
document.getElementById('input-box').dispatchEvent('DOMFocusIn');
}
else
{
document.getElementById('input-box').fireEvent('DOMFocusIn', event);
}