我想使用不是全局的javascript对象的值来创建html元素。
如果我运行以下代码,我会接受错误:" params未定义"。我想要做的只是将值作为参数复制到html代码中。
if (params.data.allowedOperations.indexOf('Put') != -1) {
return "<a onclick=\"ahey('PUT',params.data.allowedOperations)\" href=\"#\"> PUT </a>";
}
答案 0 :(得分:1)
Try
return "<a onclick=\"ahey('PUT','" + params.data.allowedOperations + "')\" href=\"#\"> PUT </a>";
As params.data.allowedOperations being a string, you need to add single quotes around the quotes. In case of number, these are not required.
答案 1 :(得分:1)
You could use:
if (params.data.allowedOperations.indexOf('Put') != -1) {
return "<a onclick=\"ahey('PUT','"+params.data.allowedOperations+"')\" href=\"#\"> PUT </a>";
}
Alternatively you could create an anchor node and then add an eventlistener to it or store the operations in a different attribute e.g.:
if (params.data.allowedOperations.indexOf('Put') != -1) {
return "<a onclick=\"ahey('PUT', this.dataset.allowedOperations)\" data-allowed-operations=\""+params.data.allowedOperations+"\" href=\"#\"> PUT </a>";
}