简体   繁体   English

在锚标记的onclick事件上未定义Javascript函数

[英]Javascript function is undefined on onclick event of anchor tag

I am not able to access javascript function in onclick event of the anchor tag. 我无法在锚标记的onclick事件中访问javascript函数。

In some cases it is working and in some cases , not. 在某些情况下,它是有效的,在某些情况下,不是。

Can any body tell why it is happening. 任何人都可以告诉它为什么会这样。

HTML CODE - HTML代码 -

<a  href='#'  class="btn waves-effect waves-light bg-blue" id="startDeviceScan" onclick="call286Dart123('CollectSysInfo', '3c6c3e33bb65e8331bf4c91c0670492e');" >start device scan </a>

Javascript function : Javascript功能:

function call286Dart123(a,b) {
    console.log(a + "\t" + b);
}

Fiddle link 小提琴链接

Fiddle Link 小提琴链接

EDIT Due to what @Dellirium pointed out in the comment, I've revised my answer to reflect the true issue here. 编辑由于@Dellirium在评论中指出,我修改了我的答案以反映这里的真实问题。 The solution would still work, however. 但是,解决方案仍然有效。

The problem is that JSFiddle makes the JavaScript run on document load by default. 问题是JSFiddle默认情况下使JavaScript在文档加载上运行。 This means that the JS is loaded after the HTML, and therefore it wouldn't have been defined at the time the onclick event was initialized in the HTML. 这意味着JS在HTML 之后加载,因此在HTML中初始化onclick事件时不会定义它。

It seems to be a problem with closures . 封闭似乎是一个问题。 If you have "Load Type" set to "onload" in JSFiddle, this is what it does internally (you can check the developer console): 如果你在JSFiddle中将“加载类型”设置为“onload”,这就是它在内部的作用(你可以检查开发者控制台):

<script type="text/javascript">
    //<![CDATA[
        window.onload=function(){
            function call286Dart123(a,b) {
                console.log(a + "\t" + b);
            }
        }
    //]]> 
</script>

This means that it is wrapped in in an anonymous event handler to the onload event. 这意味着它被包装onload事件的匿名事件处理程序中。 And since variable (and function) scopes are bound by functions in JavaScript, this makes them not available to the global scope 由于变量(和函数)作用域受JavaScript中的函数约束,因此它们不适用于全局作用域

Solution : To fix this, under the "JavaScript" menu, change "Load Type" to "No wrap - in &t;head>". 解决方案 :要解决此问题,请在“JavaScript”菜单下将“Load Type”更改为“No wrap-in&t; head>”。 It's that easy =). 这很容易=)。

See this JSFiddle fork as a working example. 将此JSFiddle fork视为一个工作示例。

In other words, this would simply change it to (again, you can verify this by visiting the developer console on my JSFiddle): 换句话说,这只是将其更改为(再次,您可以通过访问我的JSFiddle上的开发人员控制台来验证这一点):

<script type="text/javascript">
    //<![CDATA[
        function call286Dart123(a,b) {
            console.log(a + "\t" + b);
        }
    //]]> 
</script>

In which, cal286Darkt123 would be defined in the global scope (outside any other functions). 其中, cal286Darkt123将在全局范围内定义(在任何其他函数之外)。

For more info on closures, check the MDN Docs . 有关闭包的更多信息,请查看MDN文档

Don't use onclick attribute to run JavaScript if you can help it. 如果可以帮助,请不要使用onclick属性来运行JavaScript。 Instead use event listeners or JQuery on method. 而是方法使用事件侦听器或JQuery。

<a id="startDeviceScan">Do Something</a>

Vanilla JS: 香草JS:

<script>
   var el = document.getElementById("startDeviceScan");
   el.addEventListener("click", your_func, false);
</script>

JQuery: JQuery的:

$( "#startDeviceScan" ).on( "click", your_func(1234));

If you need to fetch an elements attributes for the argument such as the '1234' you can use data- attributes and reference them with JS or JQuery 如果需要获取参数的元素属性,例如'1234',您可以使用数据属性并使用JS或JQuery引用它们

JSBin example JSBin的例子

Works for me : 适合我:

 <script> function call286Dart123(a,b) { console.log(a + "\\t" + b); } </script> <a class="btn waves-effect waves-light bg-blue" id="startDeviceScan" onclick="call286Dart123('CollectSysInfo', '3c6c3e33bb65e8331bf4c91c0670492e');" href='#' >start device scan </a> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM