Javascript + queryselector support with all special characters with classname

前端 未结 2 1115
粉色の甜心
粉色の甜心 2021-01-27 02:41

Generally when using query selector in javascript we do like this,

ABCD
var className = \'abcd\';
         


        
相关标签:
2条回答
  • 2021-01-27 03:09

    Look at this answer. Which characters are valid in CSS class names/selectors? Character / is not allowed in css classes.

    0 讨论(0)
  • 2021-01-27 03:18

    You can escape it using two backslashes (\\) in your js string (it'll be evaluated as \ and finally interpreted as an escape character by CSS parser).

    var className = 'abcd\\/efgh';
    var x = document.querySelector('.' + className);
    console.log(x);
    <a href="http://www.test.com" class="abcd/efgh">ABCD</a>

    Source

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