简体   繁体   English

单击控制台中的按钮不起作用

[英]Click on a button in the console doesn't work

I have a button to change page but when I want to click on it with the console it doesn't work. 我有一个更改页面的按钮,但是当我想用控制台单击它时,它不起作用。 But with the mouse it works. 但是用鼠标可以工作。

This is the plunker : http://plnkr.co/edit/shr2xhNHzcYwTBPJlisQ?p=preview 这是pl客: http ://plnkr.co/edit/shr2xhNHzcYwTBPJlisQ?p=preview

And I do this in the console : $('#language_us').click(); 我在控制台中执行此操作: $('#language_us').click(); or $('#language_fr').click(); $('#language_fr').click(); but it doesn't change page. 但它不会更改页面。

Have you an idea ? 你有主意吗? Thanks 谢谢

You are using anchors <a> and not button <button> . 您正在使用锚点<a>而不是按钮<button>

To fire click on <a> you need to use the DOM element, jQuery object $() does not work. 要触发<a>您需要使用DOM元素,jQuery对象$()不起作用。

Use, 采用,

$('#language_us')[0].click();

.trigger() or .click() triggers a click handler defined explicitly. .trigger().click()触发明确定义一个单击处理。 You need the native click event which is the default functionality of an anchor. 您需要本地点击事件,这是锚点的默认功能。 So you need to use HTMLElement Object. 因此,您需要使用HTMLElement对象。

If you would have wrote : 如果您会写:

$('#language_fr').click(function(){
  alert('..')
})

The

$('#language_fr').click()

would have fired the alert() . 会触发alert()

Try this: 尝试这个:

$('#language_us').trigger('click');
$('#language_fr').trigger('click');

Or may be try: 或者可以尝试:

$('#language_us').get(0).click();
$('#language_fr').get(0).click();

也许尝试将标签从改变abutton或放一个button标签里面a标签并按下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM