简体   繁体   English

移动菜单上的Javascript似乎不起作用

[英]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规范 ,此属性可以具有以下值:

  • true/false : Value representing either true or false, with a default "false" value. true / false :表示true或false的值,默认值为“ false”。
  • tristate : Value representing true or false, with an intermediate "mixed" value. tristate :代表真或假的值,中间有一个“混合”值。 Default value is "false" unless otherwise specified. 除非另有说明,否则默认值为“ false”。
  • true/false/undefined : Value representing true or false, with > a default "undefined" value indicating the state or property is not relevant. true / false / undefined :代表真或假的值,>默认的“ undefined”值指示状态或属性不相关。
  • ID reference : Reference to the ID of another element in the same document ID引用 :引用同一文档中另一个元素的ID
  • ID reference : list A list of one or more ID references. ID参考 :列表一个或多个ID参考的列表。
  • integer : A numerical value without a fractional component. 整数 :不带小数部分的数值。
  • number : Any real numerical value. number :任何实数值。 string Unconstrained value type. 字符串不受限制的值类型。
  • token : One of a limited set of allowed values. 令牌 :一组有限的允许值中的一个。
  • token list : A list of one or more tokens. 令牌列表 :一个或多个令牌的列表。

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: 我认为您想要的是函数主体的结果 (将返回truefalse ,都是可以接受的值),而不是函数本身:

$this.attr( 'aria-pressed', ('false' === value ? 'true' : 'false'));

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

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