简体   繁体   English

JavaScript函数调用未触发

[英]JavaScript function call not triggering

Can anyone help in this one? 有人可以帮忙吗?

I have a simple link: 我有一个简单的链接:

<a href="javascript:ShareOnFacebook()" title="Share on Facebook"
   class="btn btn-facebook">
    <i class="fa fa-facebook"></i> Facebook
</a>

and a simple function call: 和一个简单的函数调用:

function ShareOnFacebook() {
    alert("shared");
}

And the function is just not called. 而该函数只是不被调用。 I really don't know what is going on. 我真的不知道发生了什么。 There must be a solution but I'm not seeing it. 必须有一个解决方案,但我没有看到。

The debugger says: 调试器说:

Uncaught ReferenceError: ShareOnFacebook is not defined 未捕获的ReferenceError:未定义ShareOnFacebook

UPDATE 更新

The method is called correctly when the page is loaded for the first time. 首次加载页面时,将正确调用该方法。 Every time I reload the page the method is not being called. 每次重新加载页面时,不会调用该方法。

The following seems to work fine. 以下似乎工作正常。 Just make sure you define the function in a way that your HTML can see it. 只要确保以HTML可以看到它的方式定义函数即可。 For example, before you use it in the HTML. 例如,在HTML中使用它之前。

 function ShareOnFacebook() { alert('hello') } 
 <a href="javascript:ShareOnFacebook()" title="Share on Facebook" class="btn btn-facebook"> <i class="fa fa-facebook"></i> Facebook </a> 

You use the following script instead of your html. 您使用以下脚本而不是html。

<a onclick="ShareOnFacebook()" title="Share on Facebook" class="btn btn-facebook">
    <i class="fa fa-facebook"></i> Facebook
</a>

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

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