javascript - 为什么我的函数不执行?

标签 javascript function html onload getelementbyid

我有这个代码和正文

<body onload="mail_receive()">

不会执行它。

function mail_receive() {
    document.getElementById("w")="mail receive";
}

它不会显示

<div id="w"></div>

最佳答案

您无法将字符串值分配给 DOM 元素并期望它起作用。 (事实上​​,一开始您可能无法将函数调用的结果用作 Javascript 中的左值。)

尝试

document.getElementById('w').innerHTML = 'Mail received';

关于javascript - 为什么我的函数不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814863/

相关文章:

html - 标题位于工具栏中 ionic 按钮之外的另一个高度

HTML 没有正确链接到 CSS

javascript - 在第三个子菜单上停止滑动并点击链接

javascript - Jest mock redux-store getState() 在带有 redux-mock-store 的模块中使用

java - 函数重载: Terminology for different types of overloaded functions?

c++ - 如何将数字乘以通过引用传递的函数

甲骨文 : how to ensure that a function in the where clause will be called only after all the remaining where clauses have filtered the result?

javascript - 需要用 js/php 库插入正确的方向

javascript - 控制拖放的位置

javascript - 使用递归组合数组值,即使它们是空的