Set CSS of words enclosed in double quotes

后端 未结 3 613
天命终不由人
天命终不由人 2021-01-27 04:21

This is a follow up question to my question about Setting the CSS of code if it contains a reserved word.

What I am trying to do: If some code has quot

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 04:46

    Here's a pure JavaScript version:
    id= id of element with quotes
    classid= class to add to the quotes

    function quotes(id,classid) {
        var code  = document.getElementById(id).innerHTML; 
        var split = code.split('\"'); 
        for (var j = 0; j < split.length - 1; j++) {
            if (j%2 == 0) { 
                split[j] = split[j] + '"';
            } else {
                split[j] = split[j] + '"';
            }
        }
        document.getElementById(id).innerHTML = split.join("");
        code  = document.getElementById(id).innerHTML;
        split = code.split('\'');
        var openQ = 1;
        var sub1;
        var sub2;
        for (var j = 0; j < split.length - 1; j++) {
            sub1 = split[j+1].substr(0,2);
            sub2 = split[j+1].substr(0,3);
            if(sub1 != "s " && sub2 != "ll ") {
              if (openQ) {
                split[j] = split[j] + '\'';
                openQ = 0;
              } else {
                split[j] = split[j] + '\'';
                openQ = 1;
              }
            }
            else {
                split[j] = split[j] + '\'';
            }
        }
        document.getElementById(id).innerHTML = split.join("");
    }
    

提交回复
热议问题