我今天向一些同事介绍了如何在ColdFusion中使用jQuery。这更像是对jQuery的介绍,而不是高级会话。我试图展示如何使用jQuery的$()。each()方法循环,并尝试提出一些实际的,真实世界的例子,我画了一个空白。有什么建议吗?
答案 0 :(得分:17)
// changes every other div green
$("div").each(function(i) { if (i % 2) this.style.backgroundColor = "green"; });
答案 1 :(得分:4)
略过一下。无论如何,它会让新用户感到困惑。 jQuery返回对象数组并对每个对象应用函数调用,这对于菜鸟来说并不明显。你会花时间在每个()上,而你从中得到的只有$('a').each().css("color", "red");
或$('a').each(function(){ $(this).css("color", "red");});
不要问我怎么知道noobs遇到.each()最终会犯这个错误。
答案 2 :(得分:3)
根据某个外部复选框的值
检查数据网格中的所有复选框$('#<%=dgMyDataGrid.ClientID %> :checkbox').each(function(i)
{
this.checked = $(#SelectAll).is(":checked")
});
我最初在findcontrol()方法后面有一个代码代替#SelectAll,但这有希望说明我试图做的事情。此函数也绑定到#SelectAll的click事件。
(还要注意我是一个jQuery新手!)
编辑:如果有人对此感兴趣的话,here的完整实现是{{3}}。
答案 3 :(得分:2)
您可以看到我尝试使用非常简单的“更多/更少”可扩展的报纸专栏代码。 这是使用each()函数的代码。我一直努力保持简单 - 非计数器,不存储var并且不使用索引,这里是代码:
上查看生活演示和源代码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
});
});
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 */
<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()尽可能简单。
约翰吉斯