热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从html文件调用Javascript函数

如何解决《从html文件调用Javascript函数》经验,为你挑选了1个好方法。

我有这个Javascript代码:

var cr = {};
cr.plugins_ = {};
cr.runtime = null;

cr.plugins_.Vinoos_Markets = function(runtime) {
    this.runtime = runtime;
};
(function() {
	function initialize_events(result) {
		alert(result);
	}
})();

如何通过单击按钮从html运行'initialize_events'函数?

我无权访问编辑Javascript文件.



1> T.J. Crowder..:

我没有权限编辑js文件.

然后你不能,完全停下来.对于包含它的匿名IIFE*来说,它是完全私密的.您必须将其公开为全局,以便将其与onxyz-attribute样式的事件处理程序一起使用(这需要修改Javascript代码).这是不使用它们的众多原因之一.

由于你不能在不修改Javascript的情况下做到这一点,我将假设你克服了这个限制并建议在/如果你可以修改Javascript 时该怎么做:

让IIFE挂钩按钮,data-*如果您需要特定于按钮的信息来传递它,请使用属性:

var cr = {};
cr.plugins_ = {};
cr.runtime = null;

cr.plugins_.Vinoos_Markets = function(runtime) {
  this.runtime = runtime;
};
(function() {
  function initialize_events(result) {
    alert(result);
  }
  document.getElementById("send-result").addEventListener("click", function() {
    initialize_events(this.getAttribute("data-result"));
  }, false);
}());

推荐阅读
author-avatar
淘美国
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有