我想在加载页面时使用JQuery将焦点设置在页面的第一个元素上.我可以通过使用成功设置第一个元素的焦点
$(":input:visible:first").focus();
Run Code Online (Sandbox Code Playgroud)
要么
$(':input:enabled:visible:first').focus();
Run Code Online (Sandbox Code Playgroud)
根据jquery,将焦点设置在页面上的第一个启用输入或select或textarea.
我尝试使用以下任何一种:
$('select:first').focus();
$("select").first().focus();
$(':visible:first').focus();
$('enabled:visible:first').focus();
Run Code Online (Sandbox Code Playgroud)
或者我能想到的其他变化,但没有成功.
任何人都可以帮我解决这个问题吗?我是JQuery的初学者.
我希望此功能适用于所有主流浏览器.
非常感谢你!
我正在进行一些关于网页设计的心理学实验,我想禁用鼠标点击.(请忽略可用性问题.我了解它们.我故意这样做是为了我的心理实验.)
到目前为止,我成功禁用了Firefox,Chrome和Safari中的两次点击.但是,在IE中,当我左键单击时,焦点仍会改变到我单击的位置.我想停止这种行为(即在我点击之前焦点仍然是这个地方).系统是否产生警报并不重要,因为无论如何我都会使用警报.
如果你们中的一些人能帮助我,我感激不尽.请注意,我不能使用JQuery.我的实验工具无法很好地处理JQuery.
<html>
<head>
</head>
<body>
<div>
<select>
<option> aa </option>
</select>
</div>
<div>
<select>
<option> aa </option>
</select>
</div>
<script type="text/javascript">
function mousehandler(){
alert("For the purpose of psych experiment, you can't use mouse.");
return false;
}
document.oncontextmenu = mousehandler;
document.onmousedown = mousehandler;
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我有一个JQuery代码,当输入或选择元素获得焦点时,它会执行某些操作.虽然我的输入元素代码有效,但select元素不起作用.
这就是我的工作.
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(":input").focus(function () { //this works
//do something
});
$(":select").focus(function () { //this doesn't
//do something
});
});
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
我在select上尝试了一些变体,例如使用$("select")而不是$(":select"),但是也没有用.
我是JQuery的初学者.
如果你能告诉我如何让它发挥作用我感激不尽.
非常感谢!
我有一个以下代码,我试图将onfocus事件分配给元素,以便每当元素聚焦时,调用alert.但是,警报仅在加载页面时出现,并且从此之后才会出现.
<html>
<head>
</head>
<body onload = "loadings();">
<div>
<form>
<input type="text" id="foo"/>
</form>
</div>
<script type="text/javascript">
function loadings(){
document.getElementById("foo").onfocus = alert("foo");
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
请注意,我无法做到<input type="text" id="foo" onfocus="alert('foo')"/>我想要实现的目标.
此外,这应该适用于Firefox,Chrome,IE,Safari.
提前致谢!