我目前有 2 张 Canvas ,分别展示衬衫的正面和背面。我使用了一个列表来选择是要显示正面还是背面。我只能用 anchor (#front
) 解决问题,但这意味着滚动到网页中的特定位置。我是否有其他方法来实现它并在页面加载时将焦点设置到前面的 Canvas 上?
<ul style="border:0px solid yellow; padding-left:10px;">
<li id="One">
<a href="#One" id="first" style="height:20px; width:208px; margin-bottom:-8px; padding-bottom:0px;">
<span style="margin-left:15px; font-size:medium;">Front Side</span>
</a>
<div>
<canvas id="collage" width="450" height="450" style="margin:0px; padding:0px;"></canvas>
</div>
</li>
<li id="Two">
<a href="#Two" id="sec" style="height:20px; width:208px; margin-bottom:-8px; padding-bottom:0px;">
<span style="margin-left:15px; font-size:medium;">Back Side</span>
</a>
<div>
<canvas id="collage2" width="450" height="450" style="margin:0px; padding:0px;"></canvas>
</div>
</li>
</ul>
最佳答案
你可以给 Canvas 一个标签索引:
<canvas
id="collage"
width="450"
height="450"
style="margin:0px; padding:0px;"
tabindex="1">
</canvas>
然后调用document.getElementById('collage').focus()
编辑:除了“聚焦”之外,如果问题是模拟点击和页面加载的 href,您可以使用 jQuery 执行以下操作:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#One').click();
});
</script>
关于javascript - 获得对 <canvas> 的关注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10562092/