Syntax error, unrecognized expression for href

后端 未结 4 1619
一生所求
一生所求 2020-12-17 08:31

When I add below script and run. I am getting this:

Uncaught Error: Syntax error, unrecognized expression: ul li a[href=#!id1]

相关标签:
4条回答
  • 2020-12-17 08:58

    You need to enclose special characters in quotes when using a attribute based selector.

    if ($('ul li a[href="' + id + '"]').length) {
    

    Your version of selector would result

    if ($("ul li a[href=#!...]").length) {
    

    The #! will throw unrecognized expression.


    My version where the "" escape the characters

    if ($('ul li a[href="#!..."]').length) {
    
    0 讨论(0)
  • 2020-12-17 09:00

    I got this error, because I have added these line codes somewhere in my codes;

    $("a[href^='#']").click(function(e {
        e.preventDefault();
        var position = $($(this).attr("href")).offset().top; // if you remove this line, then error won't come
    });

    0 讨论(0)
  • 2020-12-17 09:15

    you may add the below code in functions.php

    function modify_jquery() {
    if (!is_admin()) {
    	wp_deregister_script('jquery');
    	wp_register_script('jquery', 'https://code.jquery.com/jquery-1.11.3.min.js');
    	wp_enqueue_script('jquery');
    }
    }
    add_action('init', 'modify_jquery');

    0 讨论(0)
  • 2020-12-17 09:22

    I tried the solution provided by

    https://github.com/jquery/jquery/issues/2885
    

    which worked for me. I search for [href=#] in js and replace with [href*=\\#]

    a[href*=\\#]:not([href=\\#])
    
    0 讨论(0)
提交回复
热议问题