简体   繁体   English

javascript链接onclick到.js到文件

[英]javascript linking onclick to a .js to a file

I created a .js file, and then included it in the HTML pages by: 我创建了一个.js文件,然后通过以下方式将其包含在HTML页面中:

<script type="text/javascript" src="yourexternalfile.js"></script>

How do I call the function of the .js file using onclick = "...." ? 如何使用onclick = "...."调用.js文件的功能?

I know that it will be something like: 我知道它会是这样的:

<input type="BUTTON" value="Exit" onclick="javascript: ???;" >

but I can't figure it out... 但我无法弄明白......

If you are using JQuery, you can do it like this, 如果您使用的是JQuery,可以这样做,

       <input type="button" onclick="javascript: $.getScript('../scripts/yourfilepath' , function (){ youFunctionCall() ; } );" />

This will download the JS file when the button is cliked and shall execute the function called within. 这将在按钮被cliked时下载JS文件,并将执行内部调用的函数。

Say your function was ... 说你的功能是......

function myFunction() {
    alert("hello!");
}

An example of the trigger would be ... 触发器的一个例子是......

<input type="button" onclick="myFunction(); return false;" value="Click me!" />

要在外部文件中调用函数,请确保首先加载文件,然后按照正常情况调用它(只要它将自己暴露给您需要它的范围)。

I would suggest adding the on-click event in the JS file like so: 我建议在JS文件中添加on-click事件,如下所示:

html: HTML:

<input type="button" id="myButton"/>

JS file: JS档案:

var myButton = document.getElementById('myButton');
        myButton.onclick = function(){myFunction()};

This way you can program the element from the Javascript file. 这样您就可以从Javascript文件中编写元素。 Just put it in a function that you call on-load. 只需将其置于您调用的函数中即可。

include the external java script file in your page(preferably in the head tag) using 使用包含页面中的外部java脚本文件(最好在head标签中)

<script type="text/javascript" src="yourexternalfile.js"></script>

and then you can call any function defined in the js file 然后你可以调用js文件中定义的任何函数

使用<script type="text/javascript" src="">导入页面内的js文件。

Knowing when a JS file is (a) loaded, and (b) done executing is tricky, as it isn't supported by all browsers. 知道JS文件何时被加载,以及(b)完成执行是棘手的,因为并非所有浏览器都支持。

I believe you're thinking something along the lines of: 我相信你正在思考以下问题:

var s = document.createElement("script"),
    f = document.getElementsByTagName('body')[0];
s.type = 'text/javascript';
s.src = "https://ajax.googleapis.com/ajax/libs/dojo/1.5.0/dojo/dojo.xd.js";

s.addEventListener("load", function() {
   console.log("script loaded");
});

f.appendChild(s);

Like I've mentioned above, it won't work for all browsers. 就像我上面提到的,它不适用于所有浏览器。 And even if it does, it won't be very useful to you if you're actually trying to execute some code based on the JS that is pulled-in dynamically. 即使它确实如此,如果您实际上尝试基于动态引入的JS执行某些代码,对您来说也不会非常有用。

The only reliable way to execute something when a dependency is loaded is to wrap that dependency in a function. 加载依赖项时执行某些操作的唯一可靠方法是将该依赖项包装在函数中。 That way when the browser parses that JS it will execute that function, which will let you know that you can start using whatever it is that you wanted to bring in. This is exactly why JSONP works the way it works. 这样,当浏览器解析该JS时,它将执行该功能,这将让您知道您可以开始使用您想要引入的任何内容。这正是JSONP以其工作方式工作的原因。

If this is what you want to do, take a look at RequireJS . 如果这是你想要做的,请看看RequireJS

Have you tried actually putting in the name of the function? 您是否尝试过实际输入功能名称? Like onclick='myFunction()' 喜欢onclick ='myFunction()'

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

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