重复功能

Ma9*_*9ic 0 jquery

我正在学习J查询并建立一个小项目来帮助自学.

我的代码看起来很长,看起来像简单的任务,即使你一切顺利.我似乎重复了很多功能,如:

$("#england").click(function () {

    $('#englandTxt').hide();
    $('#northernIrelandTxt').hide();
    $('#walesTxt').hide();
    $('#scotlandTxt').hide();
    $('#irelandTxt').hide();
    $('#onLoad').hide();
    $("#englandTxt").fadeIn("slow");
     });  
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/fy4NP/

我将如何有效地整理这个?

thxs!

Dav*_*und 5

您的代码将受益于使用类来标识元素组,而不是必须单独访问它们.虽然有一些聪明的解决方法,但我建议在一次操作中使用类来定位类似的项目.

$('.link').click(function(){
    $('.txt').hide();
    $('#' + $(this).attr('id') + 'Txt').fadeIn();
});?
Run Code Online (Sandbox Code Playgroud)

演示