javascript 执行 onclick 事件,不点击。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15996129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing an onclick event, without clicking.
提问by user2266621
I'm using a script called photobooth for JavaScript where the user is supposed to click the camera icon to take a picture, but instead I want it to be code executed. Here is the snippet of code that I believe to be the photobooth take picture event.
我正在使用一个名为 photobooth for JavaScript 的脚本,用户应该点击相机图标来拍照,但我希望它被代码执行。这是我认为是 photobooth 拍照事件的代码片段。
var L = g("blind");
g("trigger").onclick = function () {
L.className = "blind", L.style.opacity = 1, setTimeout(function () {
L.className = "blind anim", L.style.opacity = 0
}, 50);
var e = {};
C.isActive() ? e = C.getData() : f ? e = {
x: (p - T.videoWidth) / 2,
y: (d - T.videoHeight) / 2,
width: T.videoWidth,
height: T.videoHeight
} : e = {
x: 0,
y: 0,
width: p,
height: d
};
var t = y("canvas");
t.width = e.width, t.height = e.height;
if (f) t.getContext("2d").drawImage(T, Math.max(0, e.x - (p - T.videoWidth) / 2), Math.max(e.y - (d - T.videoHeight) / 2), e.width, e.height, 0, 0, e.width, e.height);
else {
var n = x.getImageData(e.x, e.y, e.width, e.height);
t.getContext("2d").putImageData(n, 0, 0)
}
h.onImage(t.toDataURL())
};
I would like to call that event or change it so I can call it, I'm not very good with js so any help would be great!
我想调用该事件或更改它以便我可以调用它,我对 js 不是很好,所以任何帮助都会很棒!
here is the source: photobooth js
这是来源:photobooth js
回答by Netorica
is using jquery an option? you can do something like this
使用 jquery 是一种选择吗?你可以做这样的事情
$("#trigger").click();
or if you really want native js you can do something like this
或者如果你真的想要原生 js 你可以做这样的事情
document.getElementById("trigger").click();
回答by Klaster_1
Since onclick references a function, you can always call it via
由于 onclick 引用了一个函数,因此您始终可以通过以下方式调用它
document.querySelector(".trigger").onclick()
Of course, document.querySelectoris just a matter of getting DOM object with desired function, it can be referenced any other way.
当然,document.querySelector只是获取具有所需功能的 DOM 对象的问题,它可以通过任何其他方式引用。
回答by Ikrom
Photoboothcalls when the user clicks the camera icon:
Photobooth在用户单击相机图标时调用:
$('#example').photobooth().on("image",function(ev){});
To trigger click on camera icon:
触发点击相机图标:
$("#example image").click();