【问题标题】:How to Pass Dummy jQuery Object to Javascript function如何将虚拟 jQuery 对象传递给 Javascript 函数
【发布时间】:2025-11-24 18:55:02
【问题描述】:

我有一个 javascript 函数,在大多数情况下,它需要对我传递给它的 jQuery 对象做一些事情。有一个例外,该函数不需要 jQuery 对象,但是因为我已经编写它来接受字符串(命令)和 jQuery 对象,所以当我调用它时我需要一些东西来传递它。我的功能如下:

function handleNotes(command, $item) {

        var $textArea = $('#textarea_' + currentDialog); // currentDialog = global var
        var $notesDiv = $('#' + $item.attr('id') + "_notes");

    switch (command) {
        case "show":
            // do something with $notesDiv and $textArea
            break;
        case "hide":
            // do something with $notesDiv and $textArea
            });
            break;
        case "hide only":
            // do something with $textArea only
    }
}

我遇到问题的函数调用是:

handleNotes("hide only");

我试过handleNotes("hide only", null),也试过handleNotes("hide only", Object),但没有成功。有什么想法吗?

谢谢。

更新

正如许多人回答的那样,事实证明我并没有测试 $item 是否为空,所以它每次都试图设置一些东西(无论是否传递了一个对象)。我将我的功能代码更改为:

function handleNotes(command, $item) {

    var $textArea = $('#textarea_' + currentDialog); // currentDialog = global var

    if($item) {  // if not null
        var $notesDiv = $('#' + $item.attr('id') + "_notes");
    }

    switch (command) {
        case "show":
            // do something with $notesDiv and $textArea
            break;
        case "hide":
            // do something with $notesDiv and $textArea
            });
            break;
        case "hide only":
            // do something with $textArea only
    }
}

我的函数调用:handleNotes("hide only", null);

似乎工作正常。作为对我最初问题的回答,“null”似乎足以作为空白或虚拟对象,或者根本不需要传递,在这种情况下,函数会自动为其分配一个空值。感谢您的回复。

【问题讨论】:

  • if($item) 有什么问题?不能改代码什么的?

标签: javascript jquery object parameters


【解决方案1】:

您可以通过在尝试访问 attr 方法之前测试是否设置了 $item 来避免错误,如下所示:

function handleNotes(command, $item) {

if ($item) {
...

    var $textArea = $('#textarea_' + currentDialog); // currentDialog = global var
    var $notesDiv = $('#' + $item.attr('id') + "_notes");

    etc..

然后,如果您使用 handleNotes("hide only", null) 调用该方法,您将避免使用您的 jQuery 对象执行您想要执行的代码..

【讨论】:

  • 您甚至可以调用省略参数的函数:handleNotes("hide only");$item 参数将是 undefined,在 if 语句中将计算为 false
【解决方案2】:

在 JavaScript 中,调用具有较少参数的函数是可以的,然后它声明 - 但您会遇到错误,因为函数中的代码 总是 尝试访问 $item

如果你重写函数,只在需要时访问$item,你应该可以避免这些错误。

function handleNotes(command, $item) {

    var $textArea = $('#textarea_' + currentDialog); // currentDialog = global var
    var $notesDiv;
    if (command == "show" || command == "hide") {
        $notesDiv = $('#' + $item.attr('id') + "_notes");
    }

    switch (command) {
     // ...
    }
}

【讨论】:

    【解决方案3】:

    可能是因为你在用$item,不知道能不能用。试试:

    var $notesDiv = $item ? $('#' + $item.attr('id') + "_notes") : null;
    

    【讨论】:

      【解决方案4】:

      看起来您遇到了问题,因为您在未通过时尝试对项目进行汤姆访问。您应该在访问之前确保该成员存在。

      即。

      if(item && item.attr)
      

      【讨论】:

        【解决方案5】:

        你也可以像这样构造一个空的 jQuery 集合:

        jQuery([])
        

        这会给你一个零元素的集合,就像你说的jQuery("#asdfkadskfkdfas")一样。所有 jQuery 方法都可以在不抛出异常的情况下工作,尽管大多数方法会返回合法值 undefined。所以你的例子是:

        handleNotes("hide only", jQuery([]));
        

        但是,更好的解决方案是重组你的函数,正如公认的答案所暗示的那样。

        【讨论】: