jquery:如何使用 .each() 调整元素大小

发布于 2024-12-03 12:07:50 字数 3203 浏览 0 评论 0原文

再会。 我正在使用无表表单,并且想要调整 块元素的大小。我可以这样做,例如: HTML:

<div id="main-content">
  <form id="profile" blah...>
    <div id="fContainer">
      <div class="ffContainer">
        <label for='email'>Email:</label>
        <input type="text" id="email" blah...>
      </div>
      <div class="ffContainer">
        <label for='name'>Name:</label>
        <input type="text" id="name" blah...>
      </div>
    </div><!-- end fContainer -->
   </form>
</div><!-- end main-content -->

jQuery:

$(document).ready(function(){
  $(".ffContainer label").each(function(){
     if( $(this).width() > w) {
       w = $(this).width();
     };
  });
  // have widest label now make all of them the same size so that they fit
  $(".ffContainer label").attr("style","width:" + Math.round( $(window).width() / w) + "%;");

}); // end document.ready

这可以很好地完成工作。所有 字段的宽度相同,我的无表表单看起来很漂亮。

但是,如果我想在页面上添加第二个表单,我会尝试循环遍历每组表单,获取最宽的宽度,并将 设置为该宽度;然后转到页面上的下一个表单并执行相同的操作。

问题:我似乎无法使第二个表单的 宽度与第一个表单的 宽度有所不同。

我尝试了这个 HTML:

<div id="main-content">
  <form id="profile" blah...>
    <div id="fContainer">
       <div class="ffContainer">
         <label for='email'>Email:</label>
         <input type="text" id="email" blah...>
       </div>
       <div class="ffContainer">
         <label for='name'>Name:</label>
         <input type="text" id="name" blah...>
       </div>
    </div><!-- end fContainer -->
  </form>

  <form id="delta" blah...>
    <div id="fContainer">
      <div class="ffContainer">
        <label for="car">Car:</label>
        <input type="text" id="car" blah...>
      </div>
      <div class="ffContainer">
        <label for='engine'>Engine:</label>
        <input type="text" id="engine" blah...>
      </div>
    </div>
   </form>
 </div><!-- end main-content -->

这是没有完成这项工作的 jQuery:

$(document).ready(function(){
  $("#main-content #fContainer").each(function(){  // loop thru both #fContainer blocks
    $(".ffContainer label").each(function(){       // loop thru all label elements
       if( $(this).width() > w){
         w = $(this).width();
       };                                          // have widest label as var w
       $(".ffContainer label").each(function(){    // loop thru all labels again to indivdually set each attr
          $(this).attr("style","width:" + Math.round($(window).width() / w) + "%;");
       });
     }); // end .ffContainer label loop MOVE ON TO NEXT FORM GROUP
  }); // end #fContainer loop
 }); // end document.ready

任何帮助都会很棒! 上面可能有错字,但我确信我的代码中没有错字。 我真的不想将第二个表单 div 命名为其他名称并重复所有 jQuery 代码,但是如果我不必重复所有 jQuery 代码,那么将第二个 div 命名为其他名称也没有问题(希望我'我在那里说得通)。

谢谢。

Good Day.
I'm using table-less forms and want to size the <label> block-element. This I can do, like:
HTML:

<div id="main-content">
  <form id="profile" blah...>
    <div id="fContainer">
      <div class="ffContainer">
        <label for='email'>Email:</label>
        <input type="text" id="email" blah...>
      </div>
      <div class="ffContainer">
        <label for='name'>Name:</label>
        <input type="text" id="name" blah...>
      </div>
    </div><!-- end fContainer -->
   </form>
</div><!-- end main-content -->

jQuery:

$(document).ready(function(){
  $(".ffContainer label").each(function(){
     if( $(this).width() > w) {
       w = $(this).width();
     };
  });
  // have widest label now make all of them the same size so that they fit
  $(".ffContainer label").attr("style","width:" + Math.round( $(window).width() / w) + "%;");

}); // end document.ready

This does the job just fine. All the <label> fields are the same width and my table-less form looks pretty.

However, if I want to add a second form on the page, I try to loop thru each set of forms, grab the widest width, and set the <label> to that width; then move on to the next form on the page and do the same.

Problem: I can't seem to get the second form's <label> width to be anything different from the first form's <label> width.

I tried this HTML:

<div id="main-content">
  <form id="profile" blah...>
    <div id="fContainer">
       <div class="ffContainer">
         <label for='email'>Email:</label>
         <input type="text" id="email" blah...>
       </div>
       <div class="ffContainer">
         <label for='name'>Name:</label>
         <input type="text" id="name" blah...>
       </div>
    </div><!-- end fContainer -->
  </form>

  <form id="delta" blah...>
    <div id="fContainer">
      <div class="ffContainer">
        <label for="car">Car:</label>
        <input type="text" id="car" blah...>
      </div>
      <div class="ffContainer">
        <label for='engine'>Engine:</label>
        <input type="text" id="engine" blah...>
      </div>
    </div>
   </form>
 </div><!-- end main-content -->

And here's the jQuery that isn't doing the job:

$(document).ready(function(){
  $("#main-content #fContainer").each(function(){  // loop thru both #fContainer blocks
    $(".ffContainer label").each(function(){       // loop thru all label elements
       if( $(this).width() > w){
         w = $(this).width();
       };                                          // have widest label as var w
       $(".ffContainer label").each(function(){    // loop thru all labels again to indivdually set each attr
          $(this).attr("style","width:" + Math.round($(window).width() / w) + "%;");
       });
     }); // end .ffContainer label loop MOVE ON TO NEXT FORM GROUP
  }); // end #fContainer loop
 }); // end document.ready

Any help would be great!
There might be a typo above, but I'm certain there's not in my code.
I really don't want to name the second form divs something else and repeat all the jQuery code, but don't have a problem naming the second divs something else if I don't have to repeat all the jQuery code (hope I'm making sense there).

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

南风起 2024-12-10 12:07:50

修复您的 HTML(删除重复的 ID),然后尝试

$('form').each(........)

$("#main-content #fContainer").each(........)

更改

$(".ffContainer label").each(...)

$(".ffContainer label", this).each(...)

以便 jQuery 选择器一次在一个表单的范围内。

另外,在 $(".ffContainer label", this).each(...) 之前添加 var w = 0; 可能是个好主意,以确保w 的值不会从一次运行泄漏到下一次运行。

更新:我继续将您的代码插入到测试页中。从问题中很难看出一个嵌套问题。我对其进行了一些清理,下面的代码似乎可以正常工作。

$(document).ready(function(){
  $("form").each(function(){  // loop thru both #fContainer blocks
    var w = 0;
    $(".ffContainer label", this).each(function(){       // loop thru all label elements
      if( $(this).width() > w){
        w = $(this).width();
      }
    });

    var percent_width = (Math.round(w / $(window).width() * 100)) + "%"; // This calc only needs to be performed once per form.
    $(".ffContainer label", this).each(function(){    // loop thru all labels again to indivdually set each attr
      $(this).width(percent_width);
    });
  }); // end #fContainer loop
}); // end document.ready

更新:有关 jQuery 选择器上下文的详细信息
查看 这篇博文 中引用的 < a href="https://stackoverflow.com/questions/553766/jquery-selector-context-question">这个其他问题

Fix your HTML (remove the duplicate IDs), then try

$('form').each(........)

instead of

$("#main-content #fContainer").each(........)

Change

$(".ffContainer label").each(...)

into

$(".ffContainer label", this).each(...)

so the jQuery selector is within the scope of one form at a time.

Also, it might be a good idea to have var w = 0; before $(".ffContainer label", this).each(...) to ensure that the value of w doesn't leak from one run to the next.

UPDATE: I went ahead and plugged your code into a test page. There was a nesting issue that was hard to see from the question. I cleaned it up a bit and the code below appears to be working.

$(document).ready(function(){
  $("form").each(function(){  // loop thru both #fContainer blocks
    var w = 0;
    $(".ffContainer label", this).each(function(){       // loop thru all label elements
      if( $(this).width() > w){
        w = $(this).width();
      }
    });

    var percent_width = (Math.round(w / $(window).width() * 100)) + "%"; // This calc only needs to be performed once per form.
    $(".ffContainer label", this).each(function(){    // loop thru all labels again to indivdually set each attr
      $(this).width(percent_width);
    });
  }); // end #fContainer loop
}); // end document.ready

UPDATE: For details about jQuery selector context
Check out this blog post referenced in this other question

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