[英]Javascript on mobile menu doesn't seem to work
I have a problem with a mobile menu toggle not working on this site: http://www.toscanzahoeve.be 我在此网站上无法使用移动菜单切换功能时遇到问题: http : //www.toscanzahoeve.be
The mobile menu toggle (displays below 975 px) doesn't work. 移动菜单切换(显示在975像素以下)不起作用。 The javascript works for the toggle itself (gets the class 'activated') but not for the 'nav' element below (which style should switch from 'display: none' to 'display: block').
javascript适用于切换器本身(使类“已激活”),但不适用于下面的“ nav”元素(样式应从“显示:无”切换为“显示:块”)。
This is a site where it does work: http://downloadaproduct.com/ 这是一个可以正常工作的网站: http : //downloadaproduct.com/
And this is the javascript code: 这是javascript代码:
( function( window, $, undefined ) {
'use strict';
$( 'nav' ).before( '<button class="menu-toggle" role="button" aria-pressed="false"></button>' ); // Add toggles to menus
$( 'nav .sub-menu' ).before( '<button class="sub-menu-toggle" role="button" aria-pressed="false"></button>' ); // Add toggles to sub menus
// Show/hide the navigation
$( '.menu-toggle, .sub-menu-toggle' ).on( 'click', function() {
var $this = $( this );
$this.attr( 'aria-pressed', function( index, value ) {
return 'false' === value ? 'true' : 'false';
});
$this.toggleClass( 'activated' );
$this.next( 'nav, .sub-menu' ).slideToggle( 'fast' );
});
})( this, jQuery );
I spent hours trying to resolve this, any help would be greatly appreciated! 我花了数小时试图解决这个问题,我们将不胜感激!
Thanks, Stefaan 谢谢,Stefaan
You haven't specified what "doesn't work" about your code, but I do see this: 您尚未指定关于代码的“无效”信息,但我确实看到了这一点:
$this.attr( 'aria-pressed', function( index, value ) {
return 'false' === value ? 'true' : 'false';
});
Here, you are setting the value of the aria-pressed
attribute to a function (as if this were an event handler, which it is not). 在这里,您将
aria-pressed
属性的值设置为一个函数(好像这是一个事件处理程序,不是)。 The function will not actually execute in this scenario - the entire function simply becomes the value of the attribute. 该函数实际上不会在这种情况下执行-整个函数只是成为属性的值。
According to the W3C Spec , this attribute can have the following values: 根据W3C规范 ,此属性可以具有以下值:
As you can see, a function is not acceptable as the value. 如您所见,函数不可接受作为值。
I think what you want is the result of the body of your function (which will return true
or false
, both acceptable values), but not a function itself: 我认为您想要的是函数主体的结果 (将返回
true
或false
,都是可以接受的值),而不是函数本身:
$this.attr( 'aria-pressed', ('false' === value ? 'true' : 'false'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.