Jquery ReferenceError:左侧无效赋值

时间:2015-02-03 21:26:08

标签: javascript jquery

我有以下代码:

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;

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

2 个答案:

答案 0 :(得分:5)

左侧

$(this)

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

答案 1 :(得分:1)

将关闭功能更改为

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