admin管理员组文章数量:1384310
Im trying to get focus to hidden div that shows up after the .show() function is called on it. Iv tried a couple things including show().focus() or splitting it up into
$(".messageDisplayArea").show();
$("#message_display_area").focus();
the div shows up, but the page does not shift focus to it.
<script>
function showMessage(id, row) {
selectRow(row);
$(".messageDisplayArea").show();
$("#message_display_area").focus();
}
</script>
<div class="messageDisplayArea" id="message_display_area" style="display: none;">
<p> Test Test Test </p>
</div>
what am I missing? the page that this is in is decently large and the div appears at the bottom of the page. I want the browser to jump down and put the new div in focus
Im trying to get focus to hidden div that shows up after the .show() function is called on it. Iv tried a couple things including show().focus() or splitting it up into
$(".messageDisplayArea").show();
$("#message_display_area").focus();
the div shows up, but the page does not shift focus to it.
<script>
function showMessage(id, row) {
selectRow(row);
$(".messageDisplayArea").show();
$("#message_display_area").focus();
}
</script>
<div class="messageDisplayArea" id="message_display_area" style="display: none;">
<p> Test Test Test </p>
</div>
what am I missing? the page that this is in is decently large and the div appears at the bottom of the page. I want the browser to jump down and put the new div in focus
Share Improve this question asked Jul 8, 2013 at 16:21 CoffeePeddlerInternCoffeePeddlerIntern 6793 gold badges13 silver badges30 bronze badges 2-
what do you mean by wanting to put a focus in a
div
element? – Sergio Commented Jul 8, 2013 at 16:24 - maybe i am misunderstanding the .focus() function. What i want to do is give literal focus to this new div that appears. As in have the browser jump to the div since it appears at the bottom of the screen and the user doesnt even know its there until they scroll down – CoffeePeddlerIntern Commented Jul 8, 2013 at 16:27
4 Answers
Reset to default 4Replace this line:
$("#message_display_area").focus();
with this line:
window.location.hash = "message_display_area";
As far as you've given us code, what you have should work, as in this fiddle.
http://jsfiddle/SuERE/
http://jsfiddle/SuERE/1/
HTML:
<button type="button">Show and Focus</button>
<div style='height:700px'></div>
<input type="text" id="input" style='display:none;'/>
Javascript:
$("button").on("click",function(){
$("#input").show();
$("#input").focus();
});
Focus does't imply that your page is scrolled to the element and is actually often used for form controls to gain the cursor.
What you need is actually a scrollToElement() function that calculates the page offset of your newly created div and scrolls the page to that position, a good one is described here : jQuery scroll to element
Another thing you could do is add the tabindex
attribute to the div element. This will enable the focus
method to execute and find it and automatically move to it.
<div id="message-display-area" tabindex=1000>...</div>
JSFiddle to show it working.
本文标签: javascriptSetting focus to a div after showStack Overflow
版权声明:本文标题:javascript - Setting focus to a div after show - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744520428a2610420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论