在所有其他人都这样做之后,是否可以执行一个jQuery ready块.
我在我的母版页中有这个...
<script type="text/javascript">
$(document).ready(function()
{
$("input[type='text']:enabled:first", document.forms[0]).focus().select();
});
</script>
Run Code Online (Sandbox Code Playgroud)
这在我的其他页面之一......
<script type="text/javascript">
$(document).ready(function()
{
$('textarea.message-body').wysiwyg();
});
</script>
Run Code Online (Sandbox Code Playgroud)
我看到的问题是第一个块在第二个块之前执行,这导致tab键出现问题.发生的事情是,当我按Tab键时,键盘焦点会转到浏览器中的地址栏.我已经稍微更改了代码以在我的母版页中显示...
<script type="text/javascript">
$(document).ready(function()
{
var bodies = $('textarea.message-body');
if (bodies.length > 0)
{
bodies.wysiwyg();
}
$("input[type='text']:enabled:first", document.forms[0]).focus().select();
});
</script>
Run Code Online (Sandbox Code Playgroud)
并完全取消另一个准备好的块.这样做可以使Tab键正常工作,并将焦点设置到我的textarea上.
我宁愿在母版页中没有特定于页面的代码.
那么,有没有办法让我的文本框聚焦代码在wysiwyg代码之后运行?
将自定义事件触发器添加到母版页:
<script type="text/javascript">
$(document).ready(function()
{
$('textarea.message-body').wysiwyg();
$(document).trigger('tb');
});
</script>
Run Code Online (Sandbox Code Playgroud)
并将其绑定到其他页面:
<script type="text/javascript">
$(document).bind('tb', function(){
$("input[type='text']:enabled:first", document.forms[0]).focus().select();
});
</script>
Run Code Online (Sandbox Code Playgroud)
参考
这是我采用的解决方案。
在我的主页中我有这个...
<script type="text/javascript">
<!--
$(document).ready(function()
{
if (typeof (OnLoad) != "undefined")
{
OnLoad();
}
$("input[type='text']:enabled:first", document.forms[0]).focus().select();
});
//-->
</script>
Run Code Online (Sandbox Code Playgroud)
在我的详细信息页面中我有这个......
<script type="text/javascript">
<!--
function OnLoad()
{
$('textarea.message-body').wysiwyg();
}
//-->
</script>
Run Code Online (Sandbox Code Playgroud)