2 liangyu henry liangyu_henry 于 2016.09.14 09:12 提问

为什么我动态加载的 onclick 去不掉

如题,我封装了一个分页js,传入参数有请求页数,动态加载onclick方法名称。
现在遇到了问题,比如,规定每页显示10条产品。
首先进入页面默认加载全部产品分类,“下一页“按钮绑定的是 全部产品查询(第2页)方法。此时我点击产品分类筛选出A类产品,只有9个产品。”下一页“按钮就不应该有onclick,但它就是去不掉,此时点击下一页就会跳转到"全部产品查询的第二页"结果。。。。。
我尝试过removeAttr()/赋空方法/甚至把标签删了再重新添加都不管用

  //region 下一页
        if (pageInfo.nextPage != null && pageInfo.nextPage != 0) {
            $("#nextPage").attr('class', 'NextA');
            $("#nextPage").attr('onclick', methodName + '(' + pageInfo.nextPage + ',' + typeID + ',' + typename + ',' + proid + ')');
        } else {
            //$("#nextPage").remove();
            //$("#linkPage").after('<a href="javascript:void(0)" class="PreSpan" id="nextPage">下一页</a>');
            $("#nextPage").attr('onclick','function(){}')
            $("#nextPage").attr('class', 'PreSpan');
        }
        //endregion

以下是全部代码


/*参数1: 后台返回的PageInfo数据,参数2: 调用该方法的方法名(方法名不加括号'()') 参数3:类型的ID 参数4:类型名称 参数5:产品ID   param3,4,5为可选择参数*/
function pageInfoPlugin(pageInfo, methodName, typeID, tn, proid) {
    //console.log('pageNum----' + pageInfo.pageNum)
    //console.log('pageSize----' + pageInfo.pageSize)
    //console.log('total----' + pageInfo.total)
    //console   .log('pages----' + pageInfo.pages)
    //console.log('firstPage----' + pageInfo.firstPage)
    //console.log('lastPage----' + pageInfo.lastPage)
    //console.log('prePage----' + pageInfo.prePage)
    //console.log('nextPage----' + pageInfo.nextPage)
    //console.log('methodName----' + methodName)
    var typename = "'" + tn + "'";
    if (pageInfo != null) {
        //region 上一页
        if (pageInfo.prePage != null && pageInfo.prePage != 0) {
            $("#prePage").attr('class', 'NextA');
            $("#prePage").attr('onclick', methodName + '(' + pageInfo.prePage + ',' + typeID + ',' + typename + ',' + proid + ')');
        } else {
            //$("#prePage").remove();
            //$("#linkPage").before('<a href="javascript:void(0)" class="PreSpan" id="prePage">上一页</a>');
            $("#prePage").attr('onclick','function(){}')
            $("#prePage").attr('class', 'PreSpan');
        }
        //endregion

        //region 下一页
        if (pageInfo.nextPage != null && pageInfo.nextPage != 0) {
            $("#nextPage").attr('class', 'NextA');
            $("#nextPage").attr('onclick', methodName + '(' + pageInfo.nextPage + ',' + typeID + ',' + typename + ',' + proid + ')');
        } else {
            //$("#nextPage").remove();
            //$("#linkPage").after('<a href="javascript:void(0)" class="PreSpan" id="nextPage">下一页</a>');
            $("#nextPage").attr('onclick','function(){}')
            $("#nextPage").attr('class', 'PreSpan');
        }
        //endregion

        //region 链接分页计算
        $("#linkPage").children().remove();
        if (pageInfo.pageNum - 5 < 1) {
            for (var i = 1; i <= pageInfo.pages && i <= 11; i++) {
                var linkPage = '<a href="javascript:void(0)" onclick="' + methodName + "(" + i + "," + typeID + "," + typename + "," + proid + ")" + '">' + i + '</a>';

                if (pageInfo.pageNum === i) {
                    linkPage = '<a href="javascript:void(0)" class="Ahover" onclick="' + methodName + "(" + i + "," + typeID + "," + typename + "," + proid + ")" + '">' + i + '</a>';
                }

                $("#linkPage").append(linkPage);
            }
        } else {
            if (pageInfo.pageNum + 5 > pageInfo.pages) {
                for (var i = pageInfo.pages - 11 + 1; i <= pageInfo.pages; i++) {
                    var linkPage = '<a href="javascript:void(0)" onclick="' + methodName + "(" + i + "," + typeID + "," + typename + "," + proid + ")" + '">' + i + '</a>';

                    if (pageInfo.pageNum === i) {
                        linkPage = '<a href="javascript:void(0)" class="Ahover" onclick="' + methodName + "(" + i + "," + typeID + "," + typename + "," + proid + ")" + '">' + i + '</a>';
                    }

                    $("#linkPage").append(linkPage);
                }
            } else {

                for (var i = pageInfo.pageNum - 5; i <= pageInfo.pageNum + 5; i++) {
                    var linkPage = '<a href="javascript:void(0)" onclick="' + methodName + "(" + i + "," + typeID + "," + typename + "," + proid + ")" + '">' + i + '</a>';

                    if (pageInfo.pageNum === i) {
                        linkPage = '<a href="javascript:void(0)" class="Ahover" onclick="' + methodName + "(" + i + "," + typeID + "," + typename + "," + proid + ")" + '">' + i + '</a>';
                    }

                    $("#linkPage").append(linkPage);
                }
            }
        }
        //endregion

        //region 分页信息
        $("#pageText").html("当前:第【" + pageInfo.pageNum + "】页 / 总共:【" + pageInfo.pages + "】页");
        //endregion
    }

}
 <div class="met_pager">
        <a href="javascript:void(0)" class="PreSpan" id="prePage">上一页</a>
        <b id="linkPage">
            <!--<a href="" onclick="showNewsAll(1)" class="Ahover">1</a>-->
            <!--<a href="" onclick="showNewsAll(2)">2</a>-->
        </b>
        <a href="javascript:void(0)" class="NextA" id="nextPage">下一页</a>
        <span class="PageText" id="pageText">1/5</span>
    </div>

1个回答

qq_29594393
qq_29594393   Ds   Rxr 2016.09.14 09:21
已采纳

删除按钮没什么用,removeAttr()移除的是属性而不是事件
unbind移除事件,
或者禁用按钮

qq_29594393
qq_29594393 回复liangyu_henry: 浏览器请求的数据时,会先在缓存中寻找,有的话,服务器就不给于相应,响应码203,表示本地已有此文件,就不会重新获取文件,用来提高浏览器刷新的速度
大约一年之前 回复
liangyu_henry
liangyu_henry 回复当作看不见: 好
大约一年之前 回复
qq_29594393
qq_29594393 回复liangyu_henry: 清除缓存,
大约一年之前 回复
liangyu_henry
liangyu_henry 非常感谢,还有请问一下,封装的js文件修改后重启tomcat,经常出现没有及时刷新,页面跑的还是修改之前的老代码,我用的idea和tomcat8.0
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片