Javascript 如何在jQuery中组合这三个代码块并更优雅/高效地编写它们?
我有以下三段代码,我想知道是否可以以某种方式组合它们,这样我就不必一遍又一遍地编写相同的代码。对不起,我解释得很有趣。我只想学习尽可能优雅/高效地写作 HTML 2. 3.Javascript 如何在jQuery中组合这三个代码块并更优雅/高效地编写它们?,javascript,jquery,Javascript,Jquery,我有以下三段代码,我想知道是否可以以某种方式组合它们,这样我就不必一遍又一遍地编写相同的代码。对不起,我解释得很有趣。我只想学习尽可能优雅/高效地写作 HTML 2. 3. 只需在函数中换行,因此: function tweetFunc(id) { $('#download-' + id).tweetAction( { url: 'download' + id, text: 'Download ' + id,
只需在函数中换行,因此:
function tweetFunc(id) {
$('#download-' + id).tweetAction(
{
url: 'download' + id,
text: 'Download ' + id,
via: 'twittersn'
},
function() {
// When the user closes the pop-up window:
$('a.download' + id)
.addClass('active')
.attr('href','download' + id + '.psd')
;
}
);
}
for(i = 1; i <= 3; i++) {
tweetFunc(i);
}
函数tweetFunc(id){
$('#下载-'+id).tweetAction(
{
url:'下载'+id,
文本:“下载”+id,
via:‘twittersn’
},
函数(){
//当用户关闭弹出窗口时:
$('a.download'+id)
.addClass(“活动”)
.attr('href','download'+id+'.psd')
;
}
);
}
对于(i=1;我可以请你展示你的标记吗?这个问题属于你,给他们一个普通类而不是3个不同的ID,然后用迭代。每个并在你需要的地方使用索引参数+1
。@Moby'sStuntDouble我在帖子中添加了标记。@undefined:我不这么认为,不。但是现在你需要。)e> 我知道了,不测试的危险!再次感谢@plalx。我想这意味着我需要吃饭:)
很抱歉不清楚。将来可能会有更多的下载,所以我不能将数字设置为3。@Desi:所以将for
循环中的3
更改为您最终使用的编号:)
// Download 1
$('#download-1').tweetAction({
url: 'download1',
text: 'Download 1',
via: 'twittersn'
},function(){
// When the user closes the pop-up window:
$('a.download1')
.addClass('active')
.attr('href','download1.psd');
});
// Download 2
$('#download2').tweetAction({
url: 'download2',
text: 'Download 2',
via: 'twittersn'
},function(){
// When the user closes the pop-up window:
$('a.download2')
.addClass('active')
.attr('href','download2.psd');
});
// Download 3
$('#download3').tweetAction({
url: 'download3',
text: 'Download 3',
via: 'twittersn'
},function(){
// When the user closes the pop-up window:
$('a.download3')
.addClass('active')
.attr('href','download3.psd');
});
function tweetFunc(id) {
$('#download-' + id).tweetAction(
{
url: 'download' + id,
text: 'Download ' + id,
via: 'twittersn'
},
function() {
// When the user closes the pop-up window:
$('a.download' + id)
.addClass('active')
.attr('href','download' + id + '.psd')
;
}
);
}
for(i = 1; i <= 3; i++) {
tweetFunc(i);
}