简体   繁体   English

调用另一个Javascript函数时如何调用Javascript函数?

[英]How to invoke a Javascript function when another Javascript function is called?

I'm working on an application where I'm using JS to inject some elements into the DOM. 我正在使用JS将某些元素注入DOM的应用程序上。 I have a javascript function that I can't change the code for as it's part of a 3rd party install I don't have access to - it also changes the HTML content of the page when called. 我有一个无法更改代码的javascript函数,因为它是我无法访问的第三方安装的一部分-调用时它还会更改页面的HTML内容。 I can access it For simplicity's sake, I'll omit the detailed code and substitute with example code below. 我可以访问它为了简单起见,我将省略详细的代码,并用下面的示例代码代替。

cant_change() is fired during an event I don't have access to. 在我无权访问的事件中触发了cant_change()

function I can't change 我不能改变的功能

function cant_change() {
  return "can't change";
}

function I can change 我可以改变的功能

function will_change() {
  return "will change";
}

The specific question I have is: How can I fire the function will_change after cant_change fires? 我的具体问题是:我怎么能火cant_change火灾后的功能will_change? I can use jQuery too if that helps. 如果有帮助,我也可以使用jQuery。

If function cant_change doesn't have any kind of event that you can listen to, and you can replace it with another function, you could do a workaround and encapsulate it inside another function and call both cant_change and will_change: 如果function cant_change没有任何可以监听的事件,可以将其替换为另一个函数,则可以采取一种变通方法并将其封装在另一个函数中,并同时调用cant_change和will_change:

function encapsulated() {
  cant_change();
  will_change();
}

The easy answer is that you can't. 一个简单的答案就是你做不到。

However, most well designed third party JS plugins will give you the option to attach functions to various events that they will then call for you. 但是,大多数设计良好的第三方JS插件将为您提供将函数附加到各种事件的选项,然后它们将调用您。 I would suggest you look through the documentation for the plugin you're using and specifically look for events that they trigger, and how to use them. 我建议您仔细阅读所用插件的文档,并特别查找它们触发的事件以及如何使用它们。

Hopefully they will have one for what you're trying to do. 希望他们会为您想要的做一个。

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

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