简体   繁体   English

从输入图像onclick事件调用时,Javascript函数未定义

[英]Javascript function undefined when called from input image onclick event

This seems really simple but I can't get it working... 这似乎很简单,但我无法正常工作...

    function myfunction(id) 
    {
        alert("ID: " + id);
    }

There is a fiddle here http://jsfiddle.net/66ALW/ 这里有一个小提琴http://jsfiddle.net/66ALW/

The error I get in Firebug is ReferenceError: myfunction is not defined http://fiddle.jshell.net/_display/ (1) Line 1 我在Firebug中遇到的错误是ReferenceError:未定义myfunction http://fiddle.jshell.net/_display/(1 )第1行

You need set "No wrap in head" in jsfiddle to solve your problem: http://jsfiddle.net/66ALW/2/ 您需要在jsfiddle中设置“头部不缠绕”来解决您的问题: http : //jsfiddle.net/66ALW/2/

function myfunction() 
{
    alert("Reached");
}

code is ok. 代码还可以。

You are defining myfunction in the onLoad event, so it isn't being populated in the global namespace. 您是在onLoad事件中定义myfunction的,因此不会在全局名称空间中填充它。 Thus, function appears to not exist. 因此,功能似乎不存在。 There are two solutions. 有两种解决方案。

  1. Manually put it into global namespace window.myfunction = myfunction; 手动将其放入全局名称空间window.myfunction = myfunction;
  2. Configure jsfiddle to use 'no wrap' options in the frameworks and extensions section. 在框架和扩展部分中配置jsfiddle以使用“ no wrap”选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 onclick事件触发时调用的函数中的Javascript开关语句 - Javascript switch statement within a function that is called when the onclick event fires 按钮的onclick事件未调用Javascript函数 - Javascript function not getting called at the onclick event of a button 将数据推送到通过onClick事件调用的JavaScript函数 - Push Data to JavaScript Function Called with onClick Event 在锚标记的onclick事件上未定义Javascript函数 - Javascript function is undefined on onclick event of anchor tag 单击 onclick 事件(Javascript)时“x 未定义” - "x is undefined" when clicking onclick event (Javascript) 使用locatin.href =…的Javascript函数从控制台调用时有效,而不是从按钮中的onClick调用时有效 - Javascript function using `locatin.href = …` works when called from console, not when called from `onClick` in a button 当事件被另一个函数的元素调用时,JavaScript函数不起作用 - Javascript function not working when the event is called by element from another function Javascript:onclick调用时,对象中的函数未定义 - Javascript:Function in object is undefined when call by onclick 无法从onclick调用Javascript函数 - Javascript function not being called from onclick 当HTMLElement.onclick事件调用对象时,获取其函数中对象的属性-javascript - Get properties of object in its function when it called by HTMLElement.onclick event - javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM