【问题标题】:Invalid flags supplied to RegExp constructor in js.erb filejs.erb 文件中提供给 RegExp 构造函数的无效标志
【发布时间】:2015-03-03 00:00:53
【问题描述】:

我正在尝试更改我的 img(s) 的 src 属性,当它们使用 jQuery 悬停时。但我收到一条错误消息,Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'icon_history'

app.js.erb

 var hover = <%= image_path 'icon_history-h1' %>;

  var unhover = <%= image_path 'icon_history' %>;

  $('img.more').hover(function(){
    console.log(hover);
    $(this).attr('src', hover);
  },
  function(){
    console.log(unhover);
    $(this).attr('src', unhover);
  });

【问题讨论】:

    标签: javascript jquery ruby-on-rails erb


    【解决方案1】:

    你应该转义字符串并将它们放在引号中:

    var hover = '<%= j image_path 'icon_history-h1' %>';
    
    var unhover = '<%= j image_path 'icon_history' %>';
    

    【讨论】:

      【解决方案2】:

      不确定是不是你的情况,但我有类似的问题:

      SyntaxError: 提供给 RegExp 构造函数 'u' 的标志无效

      我只是通过在我的 Ubuntu 服务器中更新 Node.js 来修复它。 以前的版本是:

      $ node -v
      v5.1.1
      

      然后我做了:

      $ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
         ... long output
      $ sudo apt-get install -y nodejs
         ... shorter output
      $ node -v
      v6.7.0
      

      没有更多问题了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多