gpt4 book ai didi

javascript - 将 this 和逗号分隔值作为参数传递给函数

转载 作者:行者123 更新时间:2023-11-28 14:20:05 24 4
gpt4 key购买 nike

我有一个动态创建按钮的函数,并且 onclick 我将一些参数从 click 事件传递到 function 。我传递的参数是: this 和第二个是逗号分隔的 string 。我的问题是当我传递这个参数时我得到了这个错误:

Unexpected end of input

这就是按钮:

<button class="btnCustom" onclick="getCustomItems(this, ' + CustomIDs + ')" type="button">...</button>

CustomIDs ="3,4,5";

和函数:

function getCustomItems(e, CustomIDs) {
var IDs = CustomIDs ;
}

如何将逗号分隔的 this 对象和 CustomIDs 作为函数参数传递?

最佳答案

如果您的 ID 是动态生成的,请尝试将您的 customIds 设置为该按钮上的属性,然后只需在函数中将“this”作为关键字传递,然后在函数内部使用 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]
}

关于javascript - 将 this 和逗号分隔值作为参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55568678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com