Use a dot in a class name within jquery: $('.B.Avf').toggle();

前端 未结 2 775
陌清茗
陌清茗 2021-01-23 01:17

Is it possible to use a dot in a classname inside Jquery?

$(\'.B.Avf\').toggle();

I need to keep the dot in: B.Avf classname.
Can i som

相关标签:
2条回答
  • 2021-01-23 01:28

    You can always just use the [attr="val"] selector for this.

    $('[class*="B.Avf"]')
    

    The *= means "class contains...". Since this is looking at the attribute directly and there could be more than one class, you don't want to use =.

    0 讨论(0)
  • 2021-01-23 01:54

    There are two ways to select elements that have special selection characters in their identifiers. As Tushar mentioned in a comment, you can use a double backslash (\\) to escape the special character in query (including both jQuery and document.querySelector), or you can use an attribute selector, as Rocket Hazmat pointed out.

    Note that in CSS (that is, an actual stylesheet, not JavaScript), the selector is slightly different. You only need a single backslash to escape the special characters.

    Demo below:

    // use a double backslash to escape characters in JavaScript query selectors
    document.querySelector(".a\\.b").style.backgroundColor = "blue";
    
    //or use an attribute selector!
    document.querySelector('[class="a.b"]').style.color = "white";
    /* Use a single backslash to escape characters in CSS */
    .a\.b{border:1px solid black;}
    <div class="a.b">My class name is a.b</div>

    0 讨论(0)
提交回复
热议问题