Using TypeScript how can I trigger a function matching the onclick attribute.
使用TypeScript如何触发匹配onclick属性的函数。
<button onclick="window.open('myapp://', '_system')">Goto App</button>
So that means whenever someone clicks on button above, I will call a JavaScript function to take the user to www.google.com
这意味着只要有人点击上面的按钮,我就会调用JavaScript函数将用户带到www.google.com
Please note that I don't have control over this button or else It would have attached a method on the onclick.
请注意,我无法控制此按钮,否则它会在onclick上附加一个方法。
1 个解决方案
#1
2
I don't have control over this button
我无法控制这个按钮
-
You can get access to the button using
document.querySelectorAll
. https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll您可以使用document.querySelectorAll访问该按钮。 https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
-
Once you have access (e.g. in a variable
btn
) usebtn.addEventListener('click',()=>stuff)
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener一旦你有权访问(例如在变量btn中),使用btn.addEventListener('click',()=> stuff)https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
#1
2
I don't have control over this button
我无法控制这个按钮
-
You can get access to the button using
document.querySelectorAll
. https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll您可以使用document.querySelectorAll访问该按钮。 https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
-
Once you have access (e.g. in a variable
btn
) usebtn.addEventListener('click',()=>stuff)
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener一旦你有权访问(例如在变量btn中),使用btn.addEventListener('click',()=> stuff)https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener