jquery生成id和类

时间:2011-09-19 14:43:29

标签: jquery class

我想知道如何自动为game1,game2,game3等生成以下类...

生成有争议的课程

  • .game1-btn game1-btn,game2-btn,game3-btn,
  • .game1 - game2,game2,game3,
//Ml Game 1
$("#ml-games-collection .game1-btn").click(function () {
    $("#ml-display li.game").removeClass("hide").css("display", "none");
    $("#ml-display .game1").show();
});

$("#ml-display .game1-ss1 img").bind("click", function () {
    $("#ml-display .game1-large-display").attr({
        "alt": "img alt 1 - screenshot 1",
        "src": "img1.jpg"
    });
});
$("#ml-display .game1-ss2 img").bind("click", function () {
    $("#ml-display .game1-large-display").attr({
        "alt": "img alt 2 - screenshot 1",
        "src": "img2.jpg"
    });
});
$("#ml-display .game1-ss3 img").bind("click", function () {
    $("#ml-display .game1-large-display").attr({
        "alt": "img alt 3 - screenshot 1",
        "src": "img3.jpg"
    });
});

2 个答案:

答案 0 :(得分:0)

我不明白你的意思,但也许你应该看看attribute starts with selector

答案 1 :(得分:0)

简单:(如果我理解正确的话)

for (var i=0;i<5;i++)
{

$("#ml-games-collection .game"+i+"-btn").click(function () {
    $("#ml-display li.game").removeClass("hide").css("display", "none");
    $('#ml-display .game'+i).show();
});




}