将此值和逗号分隔的值作为函数的参数传递

我有一个可以动态创建按钮的函数,并且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作为函数的参数?


婷婷同学_
浏览 157回答 3
3回答

翻过高山走不出你

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

动漫人物

您当前正在将字符串传递+ CustomIDs +到函数中-尝试一个变量:onclick="getCustomItems(this,&nbsp;CustomIDs)"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript