6

我今天要向一些同事介绍如何在 ColdFusion 中使用 jQuery。这更像是对 jQuery 的介绍,而不是高级会话。我试图展示如何使用 jQuery 的 $().each() 方法进行循环,并试图提出一些实际的、真实的例子,但我已经画了一个空白。有什么建议么?

4

4 回答 4

17
// changes every other div green
$("div").each(function(i) { if (i % 2) this.style.backgroundColor = "green"; });
于 2009-05-14T13:07:59.723 回答
4

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

不要问我怎么知道遇到 .each() 的菜鸟最终会犯这个错误。

于 2009-05-14T13:06:28.650 回答
3

根据某些外部复选框的值检查数据网格中的所有复选框

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

我最初在 findcontrol() 方法后面有一个代码来代替 #SelectAll,但这有希望说明我正在尝试做的事情。此函数还绑定到#SelectAll 的单击事件。

(还请注意,我是 jQuery 新手!)

编辑:如果有人感兴趣,我在这里使用它的完整实现:)

于 2009-05-14T13:15:35.180 回答
2

在 JQuery 中使用 each()

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

在我的网站上查看现场演示和源代码在 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>

我试图通过使用 each() 使其尽可能简单。
约翰·吉斯

于 2010-11-27T01:34:19.590 回答