有没有一种方法可以让我们将焦点设置到 CakePHP 中的特定文本框。就像我们在谷歌主页上的文本框一样。我们加载页面并开始输入。是否可以在 Cake 中实现相同的效果。
cake 是否支持 javascript 的 onload 函数。我只需要它用于我的 View ,并且不想为此包含外部文件。我有一个用户定义的函数,可以正常工作,但 onload 不起作用。
最佳答案
只需在 head 标签之间使用 JavaScript,如下所示
<script type="text/javascript">
window.onload=function(){
document.getElementsByName('txt1')[0].focus(); // txt1 is the name of textbox
}
</script>
更新:如果您只想将其用于特定页面,则可以将其保留在该 View 中,在这种情况下,您只需将其粘贴到 HTML
之后即可> 代码如下
<script type="text/javascript">
document.getElementsByName('txt1')[0].focus(); // txt1 is the name of textbox
</script>
关于javascript - 在 CakePHP 中使用 focus() 和 onload(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11068405/