【问题标题】:Jquery ReferenceError: invalid assignment left-hand sideJquery ReferenceError:左侧分配无效
【发布时间】:2015-02-03 21:26:08
【问题描述】:

我有以下代码:

var html ="";
var $that ="";
var $url ="";

function pop_open() {
    var contents = $(this).html();

    if (contents.match("^http")) {
        console.log('contents',contents);
        $that = $(this);
        $url = contents;

        $.ajax({
            type:"POST",
            url: "AjaxUpdate/getHtml",
            context: $that,
            data: {u: 'http://stackoverflow.com'},
            dataType: 'html',
            error: function(jqXHR, textStatus, errorThrown) {
                console.log('error');
                console.log(jqXHR, textStatus, errorThrown);
            }
        }).done(function(html) {
            $link = $('<a href="myreference.html" class="p1" data-html="true" data-bind="popover">');
            $link.data('content', html);

            console.log('$link1',$link);
            $(this).html($link);
            $that = $(this);

            // Trigger the popover to open
            $link = $(this).find('a');
            $link.popover("show");
        });
    }
}

function pop_closed() {
    $(this) =$that;
    console.log('this2 ',$that);
    $link = $that.find('a');
    console.log('$link2 ',$link);
    $link.popover("hide");
    $that.html($url);
}

$('td').hover(pop_open, pop_closed);  

在控制台中,我得到:

ReferenceError: invalid assignment left-hand side
    $(this) =$that;

我收到标题中的错误。我做错了什么?

【问题讨论】:

  • 期望这个任务做什么?这是不正确的,但我不知道有什么建议可以替代。

标签: javascript jquery


【解决方案1】:

左边

$(this)

是一个函数调用,不能作为赋值语句的目标。

【讨论】:

  • 关于这个话题可以说更多,但我不知道 OP 想要它做什么。
  • 在 pop_open() 中,我试图将对表中特定 td 的引用传递给全局变量 $that。在 pop_close 中,我假设您可以将 jquery 上下文重置为 $that,以便我可以在此 td 处关闭弹出窗口。
  • 如果我做错了,你如何在 pop_close 中将上下文传递给 jquery?
  • @user61629 真的没有“jquery 上下文”这样的东西。如果$that 变量在pop_open() 代码运行后没有被触及,那么它应该仍然包含对同一个jQuery 对象的相同引用;你不需要将它分配给任何东西。就用它吧。
  • 函数 pop_closed() { $(this) =$that;在这个函数中$那个范围将不可用
【解决方案2】:

将关闭功能改为

function pop_closed() {
        $link = $(this).find('a');
        $link.popover("hide");
        $that.html($url);
    }

【讨论】:

  • 感谢 Nadeemmnn,我使用了您的代码,它确实有效。我选择 Pointy 作为正确答案,因为他回答了最初的问题。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2013-09-07
  • 2018-10-02
  • 2017-01-08
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
相关资源
最近更新 更多