我有一个声明的变量:

var recent;


我写了一个函数,需要将变量传递给它。但是,我相信正在发生的事情是将变量作为字符串传递。 var由thisVar表示:

function detachVariable(thisDiv, thisVar, fileName) {

    //Check if this section is visible:
    if($(thisDiv + " li").length > 0) {
        $(thisDiv).prepend(thisVar);
    } else {

        //If variable does not have content stored, store it:
        if(thisVar === undefined) {

            $("#site-nav li a").parents().removeClass("nav-active");
            $(this).addClass("nav-active");
            $(thisDiv + " ul.top-level").load('assets/includes/' + thisFile, function () {
                $(this).show().siblings().hide();
                ajaxAudioController();
            });

            //If variable has content, show it:
        } else {
            $("#site-nav li a").parents().removeClass("nav-active");
            $(this).addClass("nav-active");
            $(thisDiv).prepend(thisVar);
        }
    }

}


我的点击功能:

$("#site-nav .nav1").on("click", function (event) {
    detachVariable("#recent", "recent", "recent.php");
    event.preventDefault();
});


有什么方法可以将变量传递给函数吗?

最佳答案

如果我正确理解..您要做的就是删除recent周围的引号。.参见下文,

detachVariable("#recent", recent, "recent.php"); //removed quotes

09-19 15:57
查看更多