如何链接外部javascript文件onclick按钮 [英] How to link external javascript file onclick of button

查看:121
本文介绍了如何链接外部javascript文件onclick按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript文件位于Public / Scripts / filename.js中。我在template / form.tmpl.html中有一个模板文件。根文件夹包含公共和模板文件夹

The javascript file is there in Public/Scripts/filename.js. I have a template file in template/form.tmpl.html. Root folder contains Public and template folders

我想在form.tmpl.htm中点击按钮调用javascript文件。

I would like to call the javascript file onClick of a button in the form.tmpl.htm.

  <button type="button" value="Submit" onClick="Call the external js file" >

谢谢

推荐答案

我必须同意上面的评论,你不能调用文件,但你可以加载这样的JS文件,我不确定它是否回答了你的问题,但它可能有帮助......哦我在我的例子中使用了链接而不是按钮...

I have to agree with the comments above, that you can't call a file, but you could load a JS file like this, I'm unsure if it answers your question but it may help... oh and I've used a link instead of a button in my example...

<a href='linkhref.html' id='mylink'>click me</a>

<script type="text/javascript">

var myLink = document.getElementById('mylink');

myLink.onclick = function(){

    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "Public/Scripts/filename.js."; 
    document.getElementsByTagName("head")[0].appendChild(script);
    return false;

}


</script>

这篇关于如何链接外部javascript文件onclick按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆