I have tried many ways to click a button on click of other button of AngularJS.
我尝试了很多方法来点击AngularJS的其他按钮。
Dom element:
<a class="nav-bar-link" ng-click="someMethod()">
Button:
<a class="button">Log in</a>
Expected results
On click of button nav-bar-link should be clicked.
点击按钮,就可以点击链接。
Attempted Solutions
$(document).on('click', '.button', function(event) {
console.log('came');
event.preventDefault();
$(".nav-bar-link").click();
});
$(document).on('click', '.button', function(event) {
console.log('came');
event.preventDefault();
$(".nav-bar-link").triggerHandler('click');;
});
$(document).on('click', '.button', function(event) {
console.log('came');
var a = $('nav-bar-link')[0];
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
a.dispatchEvent(e);
});
$(".button").click(function() {
setTimeout(function() {
$('.nav-bar-link').trigger('click');
}, 100);
return false;
});
These answers were tested from browser console.
这些答案在浏览器控制台进行了测试。
Please let me know if there is any solution to trigger the click. Nothing worked from the above solutions.
请告诉我是否有触发点击的解决方案。从上面的解决方案中没有起作用。
I would also accept both jQuery and JavaScript solutions.
我还接受jQuery和JavaScript解决方案。
1 个解决方案
#1
3
You should specified type of selector, in your case .nav-bar-link
, change
您应该在您的案例中指定类型的选择器。
$("nav-bar-link").click();
by
通过
$(".nav-bar-link").click();
UPDATED
更新
You do not need to write much code to call an event, if you use angular you can use triggerHandler:
您不需要编写很多代码来调用事件,如果您使用角度,您可以使用triggerHandler:
$(document).on('click', '.button', function(event) {
console.log('came');
angular.element('.nav-bar-link').triggerHandler('click');
});
Result: https://jsfiddle.net/cmedina/4pkdros9/
结果:https://jsfiddle.net/cmedina/4pkdros9/
#1
3
You should specified type of selector, in your case .nav-bar-link
, change
您应该在您的案例中指定类型的选择器。
$("nav-bar-link").click();
by
通过
$(".nav-bar-link").click();
UPDATED
更新
You do not need to write much code to call an event, if you use angular you can use triggerHandler:
您不需要编写很多代码来调用事件,如果您使用角度,您可以使用triggerHandler:
$(document).on('click', '.button', function(event) {
console.log('came');
angular.element('.nav-bar-link').triggerHandler('click');
});
Result: https://jsfiddle.net/cmedina/4pkdros9/
结果:https://jsfiddle.net/cmedina/4pkdros9/