【问题标题】:Uncaught Error: Syntax error, unrecognized expression: http://www.google.com未捕获的错误:语法错误,无法识别的表达式:http://www.google.com
【发布时间】:2018-02-18 00:13:05
【问题描述】:

我在尝试将链接添加到菜单的一项时遇到问题,控制台给我以下错误 Uncaught Error: Syntax error, unrecognized expression: http://www.google.com html元素

          <nav class="main-menu">
                    <ul class="list-inline">
                        <li>
                            <a class="verde2" href="#sustancia">LA 
                               SUSTANCIA</a>
                        </li>
                        <li>
                            <a class="amarillo2" 
                            href="#vitamina">VITAMINAS</a>
                        </li>
                        <li>
                            <a href="https://www.google.com.mx">BLOG</a>
                        </li>
                        <li>
                            <a class="azul2" href="#contacto">CONTACTO</a>
                        </li>
                    </ul>
                </nav>

jquery 元素 我正在使用 jQuery v3.1.0

               $('.main-menu li a').on('click', function(e) {
                    console.log("prueba");
                    e.preventDefault();
                    var Ancla = $(this).attr('href');
                   //menu whith class sticky
                   if ($('nav').hasClass('sticky-fixed')) {
                       console.log('sticky header');
                    //height sticky menu
                   $('body,html').stop(true, true).animate({
                       scrollTop: $(Ancla).offset().top - 75
                       }, 1000);


              } else {


                  $('body,html').stop(true, true).animate({
                      scrollTop: $(Ancla).offset().top - 97
                      }, 1000);

              }

       });

谁知道我怎么解决这个问题?

【问题讨论】:

  • 您的代码中有错字,没有别的。将 $(Ancla) 替换为 $(this) 应该可以解决。在您的代码中,Ancia 不是一个 jQuery 对象,而是一个字符串 - 它包含 URL。
  • 但我声明 ancla 像 var,我尝试添加此代码来解决问题但没有解决它 $('.main-menu li').find("a[href^=' #']").on('点击', function(e) {

标签: jquery html


【解决方案1】:

您的代码中有错字。如果您将“$(Ancla)”替换为“$(this)”,它将起作用。

运行示例: https://codepen.io/Rechousa/full/KQZxYo/

运行代码:

    $('.main-menu li a').on('click', function(e) {
      console.log("prueba");
      e.preventDefault();
      var Ancla = $(this).attr('href');
      //menu whith class sticky
      if ($('nav').hasClass('sticky-fixed')) {
        console.log('sticky header');
        //height sticky menu
        $('body,html').stop(true, true).animate({
          scrollTop: $(this).offset().top - 75
        }, 1000);
      } else {
        $('body,html').stop(true, true).animate({
          scrollTop: $(this).offset().top - 97
        }, 1000);
      }
    });

编辑:

根据您的反馈,要重定向到 Google,您应该检查 URL(您的 Ancla 变量)是否为外部 URL。

鉴于您的情况,检查井号就足够了。使用这段代码,替换我之前示例中的前四行。

    console.log("prueba");
    var Ancla = $(this).attr('href');
    if(Ancla.charAt(0) !== "#") {
      return;
    }
    e.preventDefault();
    //menu whith class sticky

【讨论】:

  • 但是部分博客还没有指向谷歌
猜你喜欢
  • 2017-06-06
  • 2019-02-18
  • 2013-10-10
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 2015-11-13
  • 1970-01-01
相关资源
最近更新 更多