一:要想实现的效果
1 a:点击第三个蓝色的div自动跳转到地一个红色的div;
2 b:点击第一个红色的div自动跳转到input输入的位置
html代码
<body>
<div id="one" style="background-color: red;height: 600px; border: 1px solid;">
</div>
<div id="two" style="background-color: green;height: 600px; border: 1px solid;"></div>
<div id="three" style="background-color: blue;height: 600px; border: 1px solid;"></div>
<input type="text" id="text" value="this is text input">
</body>
二:代码实习
方案一:利用scrollIntoView()实现需求a,b
<script>
var one=document.getElementById("one")
var two=document.getElementById("two")
var three=document.getElementById("three")
var text=document.getElementById("text")
//a:点击第三个蓝色的div自动跳转到地一个红色的div;
three.onclick=function(){
one.scrollIntoView(
}
// b:点击第一个红色的div自动跳转到input输入的位置
one.onclick=function(){
text.scrollIntoView()
}
</script>
方案二:利用focus()实现需求a,b
<script>
var one=document.getElementById("one")
var two=document.getElementById("two")
var three=document.getElementById("three")
var text=document.getElementById("text")
//a:点击第三个蓝色的div自动跳转到地一个红色的div;
three.onclick=function(){
one.focus()//没有效果,因为focus只对table标签有用,实现方案是将div标签添加table属性
}
// b:点击第一个红色的div自动跳转到input输入的位置
one.onclick=function(){
text.focus()
}
</script>
//将div标签添加table属性
<div tabindex="0" id="one" style="background-color: red;height: 600px; border: 1px solid;">
//现在的one.focus()才其作用