jQuery 选择元素变量
我一直无法找到答案:
我的 HTML(简化版)看起来与此类似(页面上有几个不同的控件:文本字段、组合框等):
...
<input name="name" onfocus="showHint(this, 'Please enter your name')" onblur="hideHint()" ...>
...
javascript 函数中的 An showHint
我做了类似的事情:
function showFormEntryHint(control, text)
{
localOffset = 30;
$('#entry_hint').html(text);
var position = $(control).offset();
position.left += $(this).offset() + localOffset;
position.top -= 5;
$('#entry_hint').position(position);
$('#entry_hint').fadeIn();
}
这个想法是将 #entry_hint
div 放置在调用该函数的字段旁边。不幸的是,选择器 $(control)
似乎不起作用 - 并且它的 .offset()
总是返回 {left: 0, top: 0}.
知道如何让它发挥作用吗?非常感谢!
I haven't been able to find an answer to this:
My HTML (simplified) looks similar to this (there are several different controls on the page: text fields, comboboxes, etc.):
...
<input name="name" onfocus="showHint(this, 'Please enter your name')" onblur="hideHint()" ...>
...
An in the javascript function showHint
I do something like:
function showFormEntryHint(control, text)
{
localOffset = 30;
$('#entry_hint').html(text);
var position = $(control).offset();
position.left += $(this).offset() + localOffset;
position.top -= 5;
$('#entry_hint').position(position);
$('#entry_hint').fadeIn();
}
The idea is to position the #entry_hint
div next to the field that invoked the function. Unfortunately, selector $(control)
does not seem to work - and .offset()
on it always returns {left: 0, top: 0}
.
Any idea how I can get this to work? Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论如何,你都在使用 jQuery;为什么坚持使用 DOM0 风格的事件处理程序?
这样做:
但是,除此之外,如果您不需要太多自定义逻辑,您也可以寻找现成的插件来为您完成主要工作。
You're using jQuery anyway; why stick to DOM0-style event handlers?
Do it like this:
Barring that however, you can also look for ready-made plugins to do the brunt work for you if you don't need much custom logic.
好吧,只要摆脱控制,你就可以像使用position.left一样使用$(this)。不确定你想用 $(control) 来完成什么。
well just get rid of the control and you can use $(this) like you do for position.left. not sure what you are trying to accomplish with $(control).