繁体   English   中英

用户单击按钮时如何执行JavaScript

[英]How can i execute a javascript when a user clicks a button

我正在为Chrome创建扩展程序。 我想在用户单击按钮时执行脚本。 这是我目前在我的html文件中的代码

<input type="button" id ="button" onclick='notEmpty()' value="Play"/>

notEmpty()位于javascript(.js)文件中。 如何在notEmpty()函数中执行另一个脚本?

我尚未为chrome编写扩展程序,但对于典型的javascript,您可以执行以下操作之一。

<script type="text\javascript">
   function notEmpty()
   {
       someOtherFunction();
   }
</script>

要么

<input type="button" id ="button" onclick='notEmpty(); someOtherFunction();' value="Play"/>

如果要使用另一个文件中的功能,则需要导入这些文件:

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

但是,在Google Chrome扩展程序中,如果要在单击按钮(从扩展程序页面)时“执行”脚本,则可以使用chrome.tabs.executeScript功能

// Run that script on the current tab. The first argument is the tab id.
chrome.tabs.executeScript(null, {file: '/path/to/script.js'});

这完全取决于您的方案,当您如上所述显示“ executeScript”时,它将向该DOM中注入内容脚本 如果导入该脚本,则将仅在当前DOM中使用这些功能(在这种情况下为扩展页面,而不是选项卡)。

希望能有所帮助!

暂无
暂无

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

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