在jQuery事件中调用函数

时间:2014-01-22 01:43:03

标签: jquery

$(function () {

    alert($("#fruit").val());
    $("#fruit").click(change2());

});

var apple1 = ("<p>The orange is the fruit of the citrus species Citrus × sinensis in the family Rutaceae. The fruit of the Citrus sinensis is called sweet orange to distinguish it from that of the Citrus aurantium, the bitter orange.</p>");
var orange1 = ("<p>LOL I AM AN ORANGE </p>")
var grape1 = ("<p>A grape is a fruiting berry of the deciduous woody vines of the botanical genus Vitis. Grapes can be eaten raw or they can be used for making wine, jam, juice, jelly, grape seed extract, raisins, vinegar, and grape seed oil.</p>");
var strawberry1 = ("<p>The garden strawberry is a widely grown hybrid species of the genus Fragaria. It is cultivated worldwide for its fruit. The fruit is widely appreciated for its characteristic aroma, bright red color, juicy texture, and sweetness.</p>");
var cantaloupe1 = ("<p>Cantaloupe refers to a variety of Cucumis melo, a species in the family Cucurbitaceae. Cantaloupes range in size from 500 g to 5 kg. Originally, cantaloupe referred only to the non-netted, orange-fleshed melons of Europe.</p>");

function change2() {
    alert("Test2");
    var select = $("#fruit").val();
    switch (select) {
    case "apple":
        $("#description").html(apple1);
        alert("apple");
        break;

    case "orange":
        $("#description").html(orange1);
        alert("orange");
        break;

    case "grapes":
        $("#description").html(grape1);
        alert("grapes");
        break;

    case "cantaloupe":
        $("#description").html(cantaloupe1);
        alert.ht("melon");
        break;

    case "strawberry":
        $("#description").html(strawberry1);
        alert("straw");
        break;

    case "none":
        alert("reddit is cool");
        $("#description").html("<p> wow wow wow much doge </p>" + apple1);
        break;
    }
    alert("end");
}

我正在尝试激活switch语句,当#fruit更改选择菜单时。出于某种原因,无论我尝试什么,我都无法工作。我想把它改成var。所以我们在网页中选择apple,它应该通过switch语句运行并打印出var apple1。对于deafult,它设置为“none”,因此在启动时它会自动运行“none”情况。这只是为了测试。

1 个答案:

答案 0 :(得分:0)

这是编写事件处理函数的语法。

$("#fruit").click(change2);

删除大括号()

或者您可以编写与上面相同的更长版本。

$("#fruit").click(function() {
    change2();
});
相关问题