现在的位置: 首页 > 综合 > 正文

jquery 修改父节点的class 报错ReferenceError: Invalid left-hand side in assignment

2013年08月10日 ⁄ 综合 ⁄ 共 824字 ⁄ 字号 评论关闭

今天做一个demo,遇到修改父节点的class,先看html:

                            <span style="font-size: 100%;">
                                (
                                {% if follow_flag %}
                                <a class="unfollow" href="#">
                                    <span id="follow_txt">unfollow</span>
                                </a>
                                {% else %}
                                <a class="follow" href="#">
                                    <span id="follow_txt">follow</span>
                                </a>
                                {% endif %}
                                )
                            </span>

$(document).ready里添加 当a.follow的点击处理函数:

    $('a.follow').click(function(){
        
        /***向后台发起请求***/
       // 请求生成后立即分配处理程序,请记住该请求针对 jqxhr 对象
        var jqxhr = $.get("/kidcrate/add_follow",{followed_user_id:'{{followed_user_id}}'},function(result) {
            var obj = jQuery.parseJSON(result);  
            alert(obj.echo_msg);
            $("#follow_txt").html("unfollow");
            $('#follow_txt').parent().attr('class')="unfollow";
        });
    });

大概操作,就是当点击了 follow 之后,我把它的parent的class变成unfollow;  

结果就报错:ReferenceError: Invalid left-hand side in assignment

如果你在console里打印下面语句

 $('#follow_txt').parent().attr('class')=="unfollow";

返回true,明白了不能赋值操作。

那就麻烦一点吧,先removeClass(),然后addClass("unfollow")就成了.

抱歉!评论已关闭.