gpt4 book ai didi

javascript - 从 html 文档调用函数不会生成警报

转载 作者:行者123 更新时间:2023-11-28 04:22:55 24 4
gpt4 key购买 nike

大家好,感谢您的关注。

当我点击更新按钮时,我试图调用一个 javascript 函数。

这是javascript

var text2Array = function() {
// takes the value from the text area and loads it to the array variable.

alert("test");

}

和html

<button id="update" onclick="text2Array()">Update</button>

如果您想查看所有代码,请查看此 jsfiddle http://jsfiddle.net/runningman24/wAPNU/24/

我曾尝试使该函数成为全局函数,但不幸的是,我可以从 html 中获取警报,但由于某种原因它不会调用该函数???

最佳答案

您在 JavaScript 中声明 pswdBld 函数时出错。

...
var pswdBld() = function() {
---^^---
...

这会导致语法错误并避免加载 JavaScript 文件。

See the corrected version.


此外,您可以考虑绑定(bind)事件而不是内联它。

<button id="update">Update</button>

var on = function(e, types, fn) {
if (e.addEventListener) {
e.addEventListener(types, fn, false);
} else {
e.attachEvent('on' + types, fn);
}
};

on(document.getElementById("update"), "click", text2Array);​

See it live.

关于javascript - 从 html 文档调用函数不会生成警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13865901/

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