Javascript + queryselector support with all special characters with classname

前端 未结 2 1116
粉色の甜心
粉色の甜心 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: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);
    ABCD

    Source

提交回复
热议问题