带 Jquery 的 Foreach 语句
因此,我尝试在每次用户单击输入元素时重新定位元素。
<script>
$(document).ready(function(){
$('input').each(function(index) {
var p = $(this).attr('id');
var position = p.position();
$this.focus(function(){
$('#desired_equity_help').css("left", position.left - 435 );
$('#desired_equity_help').css("top", position.top -20 );
$('#desired_equity_help').toggle();
});
});
});
</script>
“desired_equity_help”是我尝试根据单击的输入元素切换和重新定位的元素。有什么想法为什么这不起作用吗?
这是 HTML:
<div id="desired_equity_help" class="form_tooltip" >
<div class="tooltip_inner" >
<strong>Desired Equity</strong>
<p>Hello World! This is some dummy text.</p>
</div>
</div>
<span class="apply_form_label">Desired:</span><input style="width:110px" type="text" id="desired_equity" name="desired_equity"/><br/>
<span class="apply_form_label">Break:</span><input style="width:110px" type="text" /> at: <input type="text" style="width:110px" /><select><option>Independent Users</option></select><br/>
<span class="apply_form_label">Last One:</span><textarea rows="3" cols="50"></textarea>
</div>
So I am trying to reposition an element each time a user clicks on an input element.
<script>
$(document).ready(function(){
$('input').each(function(index) {
var p = $(this).attr('id');
var position = p.position();
$this.focus(function(){
$('#desired_equity_help').css("left", position.left - 435 );
$('#desired_equity_help').css("top", position.top -20 );
$('#desired_equity_help').toggle();
});
});
});
</script>
'desired_equity_help' is the element that I am trying to toggle and reposition based on the input element clicked. Any ideas why this isn't working?
Here is the HTML:
<div id="desired_equity_help" class="form_tooltip" >
<div class="tooltip_inner" >
<strong>Desired Equity</strong>
<p>Hello World! This is some dummy text.</p>
</div>
</div>
<span class="apply_form_label">Desired:</span><input style="width:110px" type="text" id="desired_equity" name="desired_equity"/><br/>
<span class="apply_form_label">Break:</span><input style="width:110px" type="text" /> at: <input type="text" style="width:110px" /><select><option>Independent Users</option></select><br/>
<span class="apply_form_label">Last One:</span><textarea rows="3" cols="50"></textarea>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
忘记 foreach 并将事件直接应用于选择器,如下所示...
注意:使用位置将要求您的desired_equity_help元素与输入元素属于同一父元素。如果不是这种情况,请考虑使用 offset 代替
Forget the foreach and apply the event straight to the selector like so...
Note: Using position will require your desired_equity_help element to belong to the same parent as your input elements. if this is not the case consider using offset instead
这可能更接近您正在寻找的内容。您有一个未定义的变量,该元素可能应该是
show
n 而不是toggle
d。演示:http://jsfiddle.net/nEMye/
This is probably closer to what you are looking for. You had an undefined variable and the element should probably be
show
n instead oftoggle
d.Demo: http://jsfiddle.net/nEMye/
哦哦哦,看起来你在那里做了很多事情,根据你想要做的事情,你不需要那里的 each() 或 toggle() 方法。这是我要做的:
通过将 click 事件绑定到输入,您可以告诉它在每次单击输入时执行该函数。如果您不想要点击方法,可以将其更改为焦点方法或与您相关的任何方法。切换方法也是不必要的,它或多或少用于 switch 类型的按钮,单击一次,它会进入一种状态,然后再次单击它,它会返回到上一个状态状态或类似的东西。
希望这有帮助!
Ohhhhh, it looks like your doing a lot of stuff there, according to what you want to do you don't need the each() or toggle() methods there. Here is what I would do:
By binding the click event to the input you are telling it to execute that function every time the input is clicked. If you don't want the click method you can change it to the focus method or whichever method is relevant to you. The toggle method is unecessary as well, it is more or less used for switch type button, a button you would click once and it goes into one state then after you click it again it goes back to the previous state or something like that.
Hope this helps!