根据id判断需要哪个变量,并将id字符串转换为变量,赋值给layer
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="css.css"> <script src="js/jquery-1.11.3.js"></script> <script src="layer/3.03/layer.js"></script> </head> <body> <button id="applyHtml" onclick="getLayer(this)">报名</button> <button id="quoteHtml" onclick="getLayer(this)">报价</button> <button id="payHtml" onclick="getLayer(this)">缴费凭证</button> <button id="fileDownLoadHtml" onclick="getLayer(this)">文件下载</button> <button id="hintHtml" onclick="getLayer(this)">提示</button> <button id="applySuccHtml" onclick="getLayer(this)">报名成功</button> <button id="quoteSuccHtml" onclick="getLayer(this)">报价成功</button> </body> <script> //报名 var applyHtml = '报名'; //报价及标书上传 var quoteHtml = '报价及标书上传'; //费用凭证 var payHtml = '费用凭证'; //提示 var hintHtml = '提示'; //报名成功 var applySuccHtml = '报名成功'; //报价成功 var quoteSuccHtml = '报价成功'; //缴费成功 var paySuccHtml = '缴费成功'; //文件下载 var fileDownLoadHtml = '文件下载'; //打开弹出,并添加内容 function getLayer(obj){ var _this=$(obj).attr("id"); var _html=strToVar(_this); layer.open({ type: 1, title: false, closeBtn: 0, shadeClose: true, skin: 'yourclass', content: _html }); } //字符串转变为变量 function strToVar(str) { var bl = (new Function("return " + str))(); return bl; } </script> </html>