Tag: 简化

jQuery简化

我正在为一个网站编写一些代码,该网站使用4个列表项来显示/隐藏页面上不同位置的4个不同的div。 这是我正在做的一个粗略的例子: One Two Three Four This is the first div This is the second div This is the third div This is the four div 而且我有一些丑陋的JS代码正在进行中,对于我的生活,我不记得/弄清楚如何简化它。 $(“#first”).click(function() { $(“#one”).fadeIn(); $(“#two, #three, #four”).fadeOut(); }); $(“#second”).click(function() { $(“#two”).fadeIn(); $(“#one, #three, #four”).fadeOut(); }); $(“#third”).click(function() { $(“#three”).fadeIn(); $(“#two, #one, #four”).fadeOut(); }); $(“#fourth”).click(function() { $(“#four”).fadeIn(); $(“#two, #three, #one”).fadeOut(); });​ […]

jQuery代码简化

我有这个代码,但它有点重复,有没有办法让它更短? jQuery(document).ready(function() { var allTabs = jQuery(‘#front, #blog, #portfolio, #pages, #colors, #fonts’); allTabs.hide(); jQuery(‘#front-show’).click(function() { event.preventDefault(); allTabs.hide(); jQuery(‘#front’).show(); }); jQuery(‘#blog-show’).click(function() { event.preventDefault(); allTabs.hide(); jQuery(‘#blog’).show(); }); jQuery(‘#portfolio-show’).click(function() { event.preventDefault(); allTabs.hide(); jQuery(‘#portfolio’).show(); }); jQuery(‘#pages-show’).click(function() { event.preventDefault(); allTabs.hide(); jQuery(‘#pages’).show(); }); jQuery(‘#colors-show’).click(function() { event.preventDefault(); allTabs.hide(); jQuery(‘#colors’).show(); }); jQuery(‘#fonts-show’).click(function() { event.preventDefault(); allTabs.hide(); jQuery(‘#fonts’).show(); }); });

简化这个jQuery代码?

我很确定有一种方法可以缩短它,但我不知道如何。 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上。