【发布时间】:2014-04-01 22:11:01
【问题描述】:
我有一些可用的 JQuery 代码,但我一直在尝试将代码压缩为 for each 循环。如您所见,我有一个第四个 LI 的场景,但理想情况下我想要一个循环,所以它只是自动保持 LI 计数。我尝试了几种方法,但到目前为止都没有成功。这是我的工作代码:
$(document).ready(function($) {
//adds link around entire content inside of li
//li #1
var a = $('h2 a', '.slides li:first-child').clone();
a.removeAttr('title').html('');
$('.slides li:first-child').wrapInner(a);
//li #2
var b = $('h2 a', '.slides li:nth-child(2)').clone();
b.removeAttr('title').html('');
$('.slides li:nth-child(2)').wrapInner(b);
//li #3
var c = $('h2 a', '.slides li:nth-child(3)').clone();
c.removeAttr('title').html('');
$('.slides li:nth-child(3)').wrapInner(c);
//li #4 (if exists)
var d = $('h2 a', '.slides li:nth-child(4)').clone();
d.removeAttr('title').html('');
$('.slides li:nth-child(4)').wrapInner(d);
});
我还有一个基本的 JSFiddle 设置,您可以在其中使用现有代码等@http://jsfiddle.net/qgBHT/
感谢您的帮助。
【问题讨论】:
标签: jquery loops for-loop foreach