带 Jquery 的 Foreach 语句

发布于 2024-12-11 23:58:44 字数 1636 浏览 0 评论 0原文

因此,我尝试在每次用户单击输入元素时重新定位元素。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

去了角落 2024-12-18 23:58:44

忘记 foreach 并将事件直接应用于选择器,如下所示...

$('input').focus(function(){
   var p = $(this);
   var position = p.position();   
   $('#desired_equity_help').css("left", position.left - 435 );
   $('#desired_equity_help').css("top", position.top -20 );
   $('#desired_equity_help').show();                            
});

$('input').blur(function(){
    $('#desired_equity_help').hide();//hide on exit of input
});

注意:使用位置将要求您的desired_equity_help元素与输入元素属于同一父元素。如果不是这种情况,请考虑使用 offset 代替

Forget the foreach and apply the event straight to the selector like so...

$('input').focus(function(){
   var p = $(this);
   var position = p.position();   
   $('#desired_equity_help').css("left", position.left - 435 );
   $('#desired_equity_help').css("top", position.top -20 );
   $('#desired_equity_help').show();                            
});

$('input').blur(function(){
    $('#desired_equity_help').hide();//hide on exit of input
});

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

巨坚强 2024-12-18 23:58:44

这可能更接近您正在寻找的内容。您有一个未定义的变量,该元素可能应该是 shown 而不是 toggled。

$('input').each(function(index) {
    var $this = $(this);
    var position = $this.position();

    $this.focus(function() {
        $('#desired_equity_help').css({"left": position.left + 150,
                                       "top": position.top}).show();
    });
});

演示: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 shown instead of toggled.

$('input').each(function(index) {
    var $this = $(this);
    var position = $this.position();

    $this.focus(function() {
        $('#desired_equity_help').css({"left": position.left + 150,
                                       "top": position.top}).show();
    });
});

Demo: http://jsfiddle.net/nEMye/

娇俏 2024-12-18 23:58:44

哦哦哦,看起来你在那里做了很多事情,根据你想要做的事情,你不需要那里的 each()toggle() 方法。这是我要做的:

$(document).ready(function(){
    $('input').click(function(e){
        var position = $(this).position();
        $('#desired_equity_help')
        .css({"left" : position.left - 435 , 
            "top" : position.top -20 });    
    });
});

通过将 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:

$(document).ready(function(){
    $('input').click(function(e){
        var position = $(this).position();
        $('#desired_equity_help')
        .css({"left" : position.left - 435 , 
            "top" : position.top -20 });    
    });
});

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文