简化这个jQuery代码? [英] Simplify this jQuery Code?
本文介绍了简化这个jQuery代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很确定有一种方法可以缩短它,但我不知道如何。
I'm pretty sure there is a way to shorten this, but I don't know how.
img[1] = $('#img1').attr('src');
img[2] = $('#img2').attr('src');
img[3] = $('#img3').attr('src');
img[4] = $('#img4').attr('src');
title[1] = $('#title1').text();
title[2] = $('#title2').text();
title[3] = $('#title3').text();
title[4] = $('#title4').text();
desc[1] = $('#description1').attr('value');
desc[2] = $('#description2').attr('value');
desc[3] = $('#description3').attr('value');
desc[4] = $('#description4').attr('value');
url[1] = $('#url1').attr('value');
url[2] = $('#url2').attr('value');
url[3] = $('#url3').attr('value');
url[4] = $('#url4').attr('value');
已经创建了数组,我只是决定将它们排除在外,因为这里不需要它。我只是基本上把一些数据拉到了DOM。
The arrays have already be created, i just decided to exclude them from this becuase it's not really needed here. I'm just basically pulling some data the DOM.
推荐答案
for(i=1;i<=4;i++) {
img[i] = $('#img'+i).attr('src');
title[i] = $('#title'+i).text();
desc[i] = $('#description'+i).attr('value');
url[i] = $('#url'+i).attr('value');
}
这篇关于简化这个jQuery代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文