gpt4 book ai didi

javascript - 将 js 对象值作为字符串复制到 html 中

转载 作者:行者123 更新时间:2023-12-02 16:02:27 25 4
gpt4 key购买 nike

我想使用非全局的 javascript 对象的值创建 html 元素。

如果我运行以下代码,我会收到错误:“params 未定义”。我想要做的只是将值复制为 html 代码中的参数。

if (params.data.allowedOperations.indexOf('Put') != -1) {
return "<a onclick=\"ahey('PUT',params.data.allowedOperations)\" href=\"#\"> PUT </a>";
}

最佳答案

您可以使用:

if (params.data.allowedOperations.indexOf('Put') != -1) {
return "<a onclick=\"ahey('PUT','"+params.data.allowedOperations+"')\" href=\"#\"> PUT </a>";
}

或者,您可以创建一个 anchor 节点,然后向其添加事件监听器或将操作存储在不同的属性中,例如:

if (params.data.allowedOperations.indexOf('Put') != -1) {
return "<a onclick=\"ahey('PUT', this.dataset.allowedOperations)\" data-allowed-operations=\""+params.data.allowedOperations+"\" href=\"#\"> PUT </a>";
}

关于javascript - 将 js 对象值作为字符串复制到 html 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069559/

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