I have some element in a web page that I want to simulate a click on. Other elements in the same page can be easily clicked by the following logic:
$('.someClass').trigger('click');
But if I try this with a specific element, it doesn't work. I tried to add a click handler to the element that cannot be clicked, to see if the reverse was possible= Catching the a click instead of triggering one :
$('.someClass').on('click', function (event) {
console.log('element was clicked');
});
It worked. This means: The selector is correct and the element exists in the DOM. But why has my click no effect?
I tried the following all without no success:
Clicking the position where the element is located:
function simulateClick(x, y) {
$(document.elementFromPoint(x, y)).click();
}
var element = $('.someClass')[0];
var bodyRect = document.body.getBoundingClientRect(),
elemRect = element.getBoundingClientRect(),
voffset = elemRect.top - bodyRect.top;
hoffset = elemRect.left - bodyRect.left;
console.log('Element is ' + voffset + ' vertical pixels from <body>');
console.log('Element is ' + hoffset + ' horizontal pixels from <body>');
simulateClick(hoffset, voffset);
First unbinding from all events:
$('.someClass').unbind();
$('.someClass').trigger('click');
Using vanilla JS to click:
$('.someClass')[0].click();
It's obvious to me that the page somehow blocks and prevents all clicks on this element , but how can I find the mechanism which is responsible for that? The site uses obfuscated JS and I have no idea how to circumvent it?
Any ideas? (The client side is in my realm after all^^)
The event is probably not bound via jquery, but on the element. I'm not sure how that is, but this didn't work for me:
$('.mycls:eq(0)').trigger('click');
but this did:
document.querySelector('.mycls').click();
Add a click handler to the elements that you want to simulate a click on.
$('.myelementsclass').bind('click', function() {
alert($(this))
});
Then use your logic to trigger the click:
$('.myelementsclass').trigger('click');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.