代码之家  ›  专栏  ›  技术社区  ›  Hooray Im Helping codaddict

jQuery的$().each()方法在演示文稿中的实际应用

  •  6
  • Hooray Im Helping codaddict  · 技术社区  · 17 年前

    今天我将向一些同事介绍如何将jQuery与ColdFusion结合使用。这与其说是高级会话,不如说是对jQuery的介绍。我试图展示如何使用jQuery的$().each()方法进行循环,并试图给出一些实际的、真实的示例,但我还是画了一个空白。有什么建议吗?

    4 回复  |  直到 17 年前
        1
  •  17
  •   Matt Briggs    17 年前
    // changes every other div green
    $("div").each(function(i) { if (i % 2) this.style.backgroundColor = "green"; });
    
        2
  •  4
  •   user1228 user1228    17 年前

    跳过它。无论如何,这会让新用户感到困惑。jQuery返回对象数组并对每个对象应用函数调用,这对于noob来说并不明显。你会把时间花在每一件事上,而你从中得到的只是做这件事的人 $('a').each().css("color", "red"); $('a').each(function(){ $(this).css("color", "red");});

        3
  •  3
  •   Community Mohan Dere    9 年前

    根据某些外部复选框的值选中datagrid中的所有复选框

    $('#<%=dgMyDataGrid.ClientID %> :checkbox').each(function(i)
    {
    this.checked = $(#SelectAll).is(":checked")
    });
    

    我最初用一个代码隐藏findcontrol()方法来代替#SelectAll,但这很有希望说明我正在尝试做什么。此函数还绑定到#SelectAll的click事件。

    编辑:我使用它的全部实现是 here

        4
  •  2
  •   John Guise    15 年前

    您可以看到我尝试了一个非常简单的“更多/更少”可扩展的报纸专栏代码。 下面是使用each()函数的代码。我一直努力使其保持简单-无计数器,无需存储在var中,也无需使用索引,以下是代码:

    在我的网站上查看实时演示和源代码 Here at Manisa Turiksh

    JQ代码

    
    $(document).ready(function(){
    $(".expand").each(function() {
    $('p', this).hide(); //hide all p's for each div class=expand
    $('p:first', this).show(); //show only first p in each div
    $('.more',this).show(); //show more link belw each div
    });
    
    $('.more').toggle( 
        function() { 
        $(this).prevAll().show(); // toggle on to show all p's in div id=expand selected by this id=more
        $(this).html('less..'); //change legend to - less - for this id=more when div is expanded
        }, 
        function() {
        $(this).prevAll().not('p:last,h3').hide(); //hide all p's except first p, counts backwards in prevAll and reshow header h3 as it was hidden with prevAll
        $(this).html('more..'); //change legend to - more - for this id=more when div is collapsed
        });
    });
    

    CSS代码

    
    
    body {font-family:calandra;font-size:10px;}
    .expand {width:17%;margin-right:2%;float:left;} /*set div's as newspaper columns */
    p {display:block;} /*always display p's when JS is disabled */
    .more{color:red;display:none;} /*never display a's when JS is disabled */
    
    

    一些html代码

    
    <div class="expand">
    <h3>Headine 1</h3>
    <p>1.A paragraph typically consists of a unifying main point, thought, or idea accompanied by supporting details. The non-fiction paragraph usually begins with the general and moves towards the more specific so as to advance an argument or point of view.</p>
    <p>2. Each paragraph builds on what came before and lays the ground for what comes next. Paragraphs generally range three to seven sentences all combined in a single paragraphed statement. In prose fiction successive details, for example; but it is just as common for the point of a prose paragraph to occur in the middle or the end.</p>
    <p>3 A paragraph can be as short as one word or run the length of multiple pages, and may consist of one or many sentences. When dialogue is being quoted in fiction, a new paragraph is used each time the person being quoted changed.11</p>
    <p>4 The last paragraph</p>
    <a href="#" class="more">more</a>
    </div>


    <div class="expand">
    <h3>Headine 2</h3>
    <p>Amet vestibulum. Nisl a, a eros ut, nec vivamus. Tortor nullam id, metus ut pretium. Amet sociis. Ut justo. Amet a est, dolor integer, purus auctor pretium.</p>
    <p>Libero sapien sed, nulla nullam. Porta tincidunt. Suspendisse ante ac, eget fermentum vivamus. Ipsum sapien placerat. Adipiscing lorem magna, urna tortor dictum.</p>
    <p>Fringilla a morbi, sed sollicitudin magna. Justo et in, sem aenean, molestie integer tincidunt. Magna quo, erat odio. Posuere enim phasellus, dui pede. Sit a mauris, metus suscipit.</p>
    <p>Lobortis et, pellentesque nec, suspendisse elit quisque. Justo vestibulum debitis, augue fermentum. Orci et id. Ut elit, tortor ut at. Eum et non, faucibus integer nam, ac ultrices augue.</p>
    <p>Ultricies magnis, velit turpis. Justo sit, urna cras primis, semper libero quam. Lectus ut aliquam. Consequat sed wisi, enim nostrud, eleifend egestas metus. Vestibulum tristique. Et erat lorem, erat sit.</p>
    <p>Aliquam duis mi, morbi nisl. Rhoncus imperdiet pede. Sit et. Elit fusce, feugiat accumsan incididunt. Nec ipsum feugiat, accumsan dictum massa. Nec sit.22</p>
    <a href="#" class="more">more</a>
    </div>


    约翰·吉斯

    推荐文章