我有一个可以动态创建按钮的函数,并且onclick我将一些参数从click事件传递给function。我传递的参数是:this和第二个是逗号分隔的string。我的问题是,当我传递此参数时,出现此错误:输入意外结束这是按钮:<button class="btnCustom" onclick="getCustomItems(this, ' + CustomIDs + ')" type="button">...</button>CustomIDs ="3,4,5";和功能:function getCustomItems(e, CustomIDs) { var IDs = CustomIDs ;}如何传递逗号分隔的this对象并CustomIDs作为函数的参数?
3 回答

翻过高山走不出你
TA贡献1875条经验 获得超3个赞
如果您的ID是动态生成的,请尝试将customIds设置为该按钮的属性,只需在函数中传递“ this”作为关键字,然后在函数内部使用此方法从属性中获取ID。
<button class="btnCustom" onclick="getCustomItems(this)" customIDs="3,4,5" type="button">...</button>
function getCustomItems(e) {
var customIDs= e.getAttribute('customIDs');
console.log(customIds);
//3,4,5
var ids= customIds.split(',');
console.log(ids);
//[3,4,5]
}

动漫人物
TA贡献1815条经验 获得超10个赞
您当前正在将字符串传递+ CustomIDs +
到函数中-尝试一个变量:
onclick="getCustomItems(this, CustomIDs)"
添加回答
举报
0/150
提交
取消