简体   繁体   English

使用 onclick 运行 Javascript?

[英]Run Javascript using onclick?

If I set the following up:如果我设置以下内容:

<script type="text/javascript" src="https://test.com/abc"></script> 

Between the head tags, the code runs when the page starts up.在 head 标记之间,代码在页面启动时运行。 Is there any way of stopping this from running during startup and instead running it using the onclick method from a html button?有什么方法可以阻止它在启动期间运行,而是使用 html 按钮的 onclick 方法运行它?

Take the javascript in your file and wrap it in a function.在您的文件中获取 javascript 并将其包装在一个函数中。 You can then assign that function to the onclick attribute of a button.然后,您可以将该功能分配给按钮的onclick属性。

<button onclick="myFunction()">Click Me</button>

And your script还有你的剧本

function myFunction() { 
    //your existing code goes here
}

Here's a fiddle .这是一个小提琴

1- You should try to avoid adding scripts in the head as it makes slow rendering page. 1- 您应该尽量避免在头部添加脚本,因为它会使页面渲染速度变慢。 If you really need to do so may be you could look for defer / async scripts.如果你真的需要这样做,你可以寻找延迟/异步脚本。

2- You should try to put the script at bottom in order to make browser rendering all the html and css as fast as possible. 2- 您应该尝试将脚本放在底部,以使浏览器尽可能快地呈现所有 html 和 css。

3- Finally at the bottom you should call in the load page. 3- 最后,您应该在底部调用加载页面。 For example in jquery.ready like this doc says, search for jquery ready event.例如在 jquery.ready 就像这个文档说的那样,搜索 jquery ready 事件。 In that moment you should add a listener to the button you need by calling a jquery selector on the button, search for jquery selector, finally you add the click event, search for jquery click event, and then you process what you need inside that callback.在那一刻,您应该通过在按钮上调用 jquery 选择器,为您需要的按钮添加一个侦听器,搜索 jquery 选择器,最后添加点击事件,搜索 jquery 点击事件,然后在该回调中处理您需要的内容.

Here is a fiddle.这是一个小提琴。

[https://jsfiddle.net/d6zfqpc6/][1]

Jquery acts as a good polyfill also to somethings :). Jquery 也作为一个很好的 polyfill 对某些东西:)。

Although the onclick method is a very nice way, but it is no longer supported .虽然onclick方法是一种很不错的方式,但是已经不再支持了

The addEventListener method is the most suitable and the modern method to do it. addEventListener方法是最合适和最现代的方法。

It can be used in two ways:它可以通过两种方式使用:

  1. Connecting an already existing function to an event listener:将已经存在的函数连接到事件侦听器:

     document.getElementByID("clickMe").addEventListener("click", myfunction);
  2. Writing a function within an event listener:在事件侦听器中编写函数:

     document.getElementByID("clickMe").addEventListener("click", function() { //a function, for eg.: document.getElementById("p").innerHTML = "Hello World"; });

Here's an example snippet for ease of understanding:这是一个易于理解的示例片段:

 document.getElementById("clickme").addEventListener("click", function() { document.getElementById("p").innerHTML = "Hello World"; });
 <button id="clickme">Try it</button> <p id="p"></p>

Also, I'd advice you to use the script at the bottom of the page rather than the head, so that the HTML and CSS can load first.另外,我建议您在页面底部而不是头部使用script ,以便首先加载 HTML 和 CSS。


Hope I could help.希望我能帮上忙。

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

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