gpt4 book ai didi

javascript - 无法附加到 javascript 中的表。错误 : Cannot assign to a function result

转载 作者:行者123 更新时间:2023-11-30 17:26:15 25 4
gpt4 key购买 nike

我正在使用 ajax 从 Web API 获取项目,然后尝试将该项目的属性分配给表:

 function find() {
var id = $('#contentID').val();
$.getJSON(uri + '/' + id)
.done(function (data) {
fillTable(data);
})
.fail(function (jqXHR, textStatus, err) {
$('#content').text('Error: ' + err);
});
}


function fillTable(item) {
document.getElementById("mybody") += "<td id = \"td1\" runat=\"server\">" + item.contentType + "</td> <td id=\"td2\" runat=\"server\">" + item.contentID + "</td></tr>";
}

我的 html 中有这个表:

 <table id="example" class="responstable">
<thead>
<tr>
<th data-th="Driver details"><span>Timestamp</span></th>
<th>Content Type</th>
<th>Task Name</th>
<th>Task Version</th>
<th>Task State</th>
</tr>
</thead>

<tbody id="mybody" runat="server">
<tr>

</tr>
</tbody>

</table>

我知道我正在取回 JSON 对象,因为我将它的属性打印到屏幕上并且它们打印正常。我不明白为什么 javascript 不会将它们附加到表中。

最佳答案

函数调用结果的赋值没有意义并且被禁止,并出现运行时异常。

=(赋值)运算符,以及像 += 这样的派生词,要求运算符的左侧是一个表达式的 Reference Specification Type .也就是说,lhs = e 仅在作为 lhs 与“变量或属性表达式”一起使用时才有效。

The Reference type is used to explain the behaviour of such operators as delete, typeof, and the assignment operators [including =, +=, etc]. For example, the left-hand operand of an assignment is expected to produce a reference [specification type expression].

这就是 JavaScript 可以将值分配回适当的变量/属性的方式。当无法进行此类分配时会引发运行时错误,例如尝试在 = 运算符的左侧使用函数调用时。

(这是我对 ++ 的回答的改编。)


此特定实例可以通过以下方法解决,满足上述要求,因为 innerHTML 是一个属性

 var elm = document.getElementById("mybody");
elm.innerHTML += "the new stuff";

(但是,更好的是使用正确的 DOM 操作,而不是 HTML 处理。)

关于javascript - 无法附加到 javascript 中的表。错误 : Cannot assign to a function result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24196042/

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