简体   繁体   English

单击按钮后,按钮将不会进入网址

[英]Button won't go to a url when clicked

Having problems with my js buttons. 我的js按钮有问题。 Right now, they work perfectly, but I am trying to turn my "Order Online" button into a link that opens up a new window to a Menufy menu page. 现在,它们可以正常工作,但是我试图将“在线订购”按钮变成一个链接,该链接打开一个新窗口,进入Menufy菜单页面。 However, I can't seem to figure it out. 但是,我似乎无法弄清楚。 Right now the buttons are set up as tabs, basically. 现在,基本上将按钮设置为选项卡。

<section class="menu_options"> 
      <button class="menu_btn" onclick="displayMenu('dine-in');">Dine in</button> 
      <button class="menu_btn" onclick="displayMenu('take-away');">Take Away</button>
      <button class="menu_btn" onclick="displayMenu('lunch');">Lunch</button> 
      <button class="menu_btn" onclick="displayMenu('online');">Order Online</button> 
</section>

displayMenu = function(menu){
  $(".menu_inner").find(".menu-active").toggleClass("menu-active").slideToggle();
  $(".menu_inner").find("#"+menu).toggleClass("menu-active").slideToggle();
}

To change your button into a link, you can try it as follows 要将按钮更改为链接,可以尝试以下操作

<a href="javascript:void(0)"  onclick="displayMenu('online');">Order Online</a>

The void operator evaluates the given expression and then returns undefined. void运算符计算给定的表达式,然后返回undefined。

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). void运算符通常仅用于获取未定义的原始值,通常使用“ void(0)”(等效于“ void 0”)。 In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value). 在这些情况下,可以改用undefined全局变量(假设尚未将其分配给非默认值)。

The reason why void(0) is used is because, if it isnt specified, the default behaviour of <a> , that is redirecting to the href specified will be nullified and the onclick specified will be called 使用void(0)的原因是,如果未指定它,则重定向到指定href的默认行为<a>将被无效,并且将调用指定的onclick

Using this code "window.location.href='url'" works to take you to a new url within your window, ie: 使用此代码“ window.location.href ='url'”可将您带到窗口中的新URL,即:

<button onclick="window.location.href='url.com'">Click me</button>

Whereas, if you want to open the link into a new window, use this: 而如果您想在新窗口中打开链接,请使用以下命令:

<button class="menu_btn" onclick="window.open('url.com', '_blank')">Lunch</button> 

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

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