从json返回一个这样的字串,如何把它拼接到onclick里?

字符串值为:xx,"><xx


function escapeHtml(string) {


    var entityMap = {

        "&": "&amp;",

        "<": "&lt;",

        ">": "&gt;",

        '"': '&quot;',

        "'": '&#39;',

        "/": '&#x2F;'

    };

    return String(string).replace(/[&<>"'\/]/g, function (s) {

        return entityMap[s];

    });

};

...

 var esctext = escapeHtml(v.desctext);

                        str += "<tr><td>" + esctext + "</td>";                            

                        str += '<td><button class="layui-btn layui-btn-danger" onclick="deleteitem(' + type + ',\'' + esctext + '\')">刪除</button>';

                        

用了escape后td的显示逻辑部分正常,但删除按钮那里不正确,也没法传正确数据到后台,用firefox内视是这样的内容


引用文字

=>onclick="deleteitem(0,'xx,"><xx')"


浮云间
浏览 387回答 1
1回答

LEATH

'可以去掉,最外面的引号统一用单引号试试那你可以参照以下小栗子:<!DOCTYPE html><html><head>&nbsp; &nbsp; <title></title></head><body>&nbsp; &nbsp; <div id="box"></div>&nbsp; &nbsp; <script type="text/javascript" src="jquery-3.2.1.min.js"></script>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; var a = 'one', b = 'two';&nbsp; &nbsp; &nbsp; &nbsp; function test(x, y) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '<button onclick="show(\'' + x + '\',\'' + y + '\')">Click Me</button>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function show(x, y) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(x + '-' + y);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $('#box').append(test(a, b));&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript