动态建立对jquery插件的调用

时间:2010-12-10 08:53:21

标签: jquery jquery-plugins

我想执行以下操作来构建对jquery插件的调用

jQuery("#wrapper").myPLugin({
    direction: direction,
    switch myOption
    {
        case "small":
        items:10,

        case "big":
        items:25,

    }
    switch otherOption
    {
        case "small":
            prev: {
            button: buttonPrev,
            key: "left"
        },

        case "big":
            prev: {
            button: buttonPrev,
            key: "left"
        },

    }
    pagination: ".paginationHolder"
});

显然这不起作用,但是这种构建的最佳方法是什么,是否有一种通用的方法来动态建立对这样的jQuery插件的调用

1 个答案:

答案 0 :(得分:1)

选项1

三元运营商是你的朋友

condition ? trueResult : falseResult

所以在你的情况下可能是

jQuery("#wrapper").myPLugin({
    direction: direction,
    items: myOption == "small" ? 10 : myOption == "big" ? 25 : 0, // or some default value
    prev: otherOption == "samll" ? {button: prevButton, key: 'left'} : otherOption == "big" ? {button: prevButton, key: 'left'} : null,
    pagination: ".paginationHolder"
});

选项2

另一种选择是在所有javascript对象完全动态输入后,在调用之外构建对象:

var setup = new object();
setup.direction = "foo";
switch(myOption){
  case "small":
    setup.items = 10;
    break;
  case "big":
    setup.items = 25;
    break;
}
switch(otherOption){
  case "small":
    setup.prev = {button: prevButton, key: 'left'};
    break;
  case "big":
    setup.prev = {button: prevButton, key: 'left'};
    break;
}
setup.pagination =".paginationHolder"

jQuery("#wrapper").myPLugin(setup);
相关问题