最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simulate user click in chrome console - Stack Overflow

programmeradmin4浏览0评论

Why can't I trigger or simulate user click using chrome's console in browser? like I have a link on a page I do $('#app .mylink').click() it should go somewhere.

Why can't I trigger or simulate user click using chrome's console in browser? like I have a link on a page I do $('#app .mylink').click() it should go somewhere.

Share Improve this question edited Dec 14, 2017 at 3:50 KARTHIKEYAN.A 20.1k9 gold badges135 silver badges149 bronze badges asked Dec 14, 2017 at 3:00 Casa LimCasa Lim 3753 gold badges5 silver badges15 bronze badges 3
  • You can. Is your selector correct and do they have jQuery? If not, use vanilla JS – Andrew Li Commented Dec 14, 2017 at 3:01
  • 1 Chrome's inspector has a jQuery-like helper ($), but that's it. You'd need to implement it yourself when interacting within the page as @Li357 said. – Phix Commented Dec 14, 2017 at 3:04
  • @Phix what do you mean by implement myself? isn't $('#app .mylink').click() is firing click already on an element? – Casa Lim Commented Dec 14, 2017 at 3:14
Add a comment  | 

1 Answer 1

Reset to default 16

$ in Chrome's console is an alias for document.querySelector(), except when it's not. If $ is declared in the page, usually by jQuery, $ in the console will point to that instead.

Calling click on a jQuery object representing an a element won't perform the native navigation, but calling click on the native HTMLElement will. If you know the page you're working with uses jQuery, you'll need to retrieve the native HTMLElement from the jQuery object:

$('#app .mylink')[0].click(); // assuming you want to click the first element returned

But if jQuery's not involved, that won't work. Best to be unambiguous:

document.querySelector('#app .mylink').click();
发布评论

评论列表(0)

  1. 暂无评论