Javascript 如何在父div内设置div的焦点?
这是我的html代码,当页面加载时,焦点应该放在第二个div上,但这不起作用,请帮助我如何执行此操作,提前感谢。。焦点不会滚动到指定的div。使用:Javascript 如何在父div内设置div的焦点?,javascript,jquery,html,Javascript,Jquery,Html,这是我的html代码,当页面加载时,焦点应该放在第二个div上,但这不起作用,请帮助我如何执行此操作,提前感谢。。焦点不会滚动到指定的div。使用: <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://ajax.googlea
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#container2").focus();
});
</script>
</head>
<body>
<div id="mainwrapper">
<!-- container1 -->
<div id="container1"
style="height:100px;width:299px;">
First Lorem Ipsum is simply dummy text1
</div>
<!-- container2 -->
<div id="container2" style="height:100px;width:299px;">
Second Lorem Ipsum is simply dummy text2
</div>
</div>
</body>
这可能正是你需要的。首先,将class=borderonload设置为container2,然后添加以下CSS:
$(document).ready(function() {
$('html, body').animate({ scrollTop: $('#container2').offset().top }, 'slow');
});
手稿:
#mainwrapper div:hover, .borderonload {
border: 2px solid #aaf;
}
.你说的专注是什么意思?你的意思是滚动到它?div没有焦点,除非它们是内容可编辑的。focus的意思是在这里悬停你想让鼠标指针停留在那个元素上吗?我需要悬停焦点在那个div上我不需要滚动,我需要悬停焦点在container2@user3467244:哇。。。什么是悬停焦点?翻页时需要div2的大纲loads@user3467244:您可以通过设置边框来完成此操作。例如@user3467244,速度很快,谢谢:。我改进了代码,现在它也删除了无用的事件侦听器。
$('#mainwrapper').on('mousemove', function (e) {
var $cont2 = $('#container2');
if (e.target !== $cont2) {
$cont2.removeClass('borderonload');
$('#mainwrapper').off('mousemove');
}
});