admin管理员组文章数量:1323029
I have a function for renaming certain divs. The way I'm trying to get it to work is like this:
- User right clicks div
- User clicks 'Rename' on context menu
- Div gets an input element and is automatically focused
- The input gets submitted after pressing enter or clicking the screen
I have most of the steps done but the input element is not being focused after I click 'rename'. Here's the code:
function Rename( ){
ClickedFile.innerHTML = "<input class='Rename' type='text' value='" + ClickedFile.innerHTML + "'>";
ClickedFile.childNodes[0].focus();
}
The ClickedFile is the node that was right clicked. Changing The innerHTML works fine but the .focus() does not and I'm not sure why. I don't get any errors on the console, either.
I've also tried using:
ClickedFile.childNodes[0].select();
ClickedFile.childNodes[1].focus();
ClickedFile.focus();
None of them have worked.
Edit:
I know using JQuery might help, but I'm more interested in finding out why this isn't working.
I fixed the problem. It has something to do with event handlers. My answer is posted below.
I have a function for renaming certain divs. The way I'm trying to get it to work is like this:
- User right clicks div
- User clicks 'Rename' on context menu
- Div gets an input element and is automatically focused
- The input gets submitted after pressing enter or clicking the screen
I have most of the steps done but the input element is not being focused after I click 'rename'. Here's the code:
function Rename( ){
ClickedFile.innerHTML = "<input class='Rename' type='text' value='" + ClickedFile.innerHTML + "'>";
ClickedFile.childNodes[0].focus();
}
The ClickedFile is the node that was right clicked. Changing The innerHTML works fine but the .focus() does not and I'm not sure why. I don't get any errors on the console, either.
I've also tried using:
ClickedFile.childNodes[0].select();
ClickedFile.childNodes[1].focus();
ClickedFile.focus();
None of them have worked.
Edit:
I know using JQuery might help, but I'm more interested in finding out why this isn't working.
I fixed the problem. It has something to do with event handlers. My answer is posted below.
- @tadman jQuery?? What the heck is this for? It is like saying Why you're not using CodeIgniter in the first place. It makes very hard to get wrong. – Jan Turoň Commented May 13, 2013 at 21:36
- 2 What you have seems to work fine for me here. – Ja͢ck Commented May 13, 2013 at 21:41
- I'm not using it so that I get a better understanding of pure javascript. I'd really rather not debate here whether or not I should be using jQuery anyway – tay10r Commented May 13, 2013 at 21:41
- @tadman Read my last ment – tay10r Commented May 13, 2013 at 21:54
- 2 Since this is a JavaScript question, policy seems to be: Don't answer it telling the asker to use jQuery. Just answer with JavaScript, unless jQuery or other frameworks are explicitly requested, or named as being used. – doppelgreener Commented May 13, 2013 at 23:00
3 Answers
Reset to default 5You have to add the element as part of DOM
var input = document.createElement("input");
input.className = "Rename";
input.type = "text";
document.getElementById("somenode").appendChild(input);
input.focus(); // should work now
see the fiddle
The reason is that since you're immediately invoking the select and focus methods. The browser didn't have a chance yet to insert the element so it's not part of the DOM.
A quick and dirty solution is to use the setTimeout
-function with a value of 0 milliseconds:
function Rename( ){
ClickedFile.innerHTML = "<input class='Rename' type='text' value='" + ClickedFile.innerHTML + "'>";
setTimeout(function(){ClickedFile.childNodes[0].focus();}, 0);
}
This causes the browser to do everything it has to do, and then run the function you passed into the setTimeout
The following fiddle shows your example in a working state: http://jsfiddle/Kennethtruyers/fX8h6/
I fixed the problem by changing the function that renames the div to be triggered on 'mouseup' instead of 'mousedown'.
I think the reason this was causing a problem is because the Rename() function was being triggered on mousedown, causing the focus to be set during 'mousedown', but the contextmenu wasn't being hidden until 'mouseup'.
I can't confirm that's the reason but I do know that the code works after 'mousedown' was changed to 'mouseup' for the trigger of Rename().
本文标签: javascriptfocus() Not Focusing On New ElementStack Overflow
版权声明:本文标题:javascript - .focus() Not Focusing On New Element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742107515a2421090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论