javascript 选择CSS中的元素

p8h8hvxi  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(154)

给定HTML中的以下代码:

<body>
    <pre class="typescript">
      <span class="classDecl">    
        <span class="statement">export</span> 
        <span class="keyword">class</span> 
        <span class="identifier">Sample</span> 
        <span id="open1">{</span>
        <span class="keyword">private</span> 
        <span class="marked">
          <span class="identifier">name</span>: 
          <span class="type">string</span>
        </span>;
        <span class="ctorDecl">
          <span class="keyword">constructor</span>
          <span id="open2">(</span>
          <span class="marked">
            <span class="identifier unused">name</span>: 
            <span class="type">string</span>
          </span>
          <span id="close2">)</span> 
          <span id="open3">{</span>
          <span class="keyword">this</span>.
          <span class="identifier">name</span> = 
          <span class="error">
            <span class="identifier">name1</span>
          </span>;
          <span id="close3">}</span>
        </span>
        <span class="methodDecl">
          <span class="identifier">greeting</span>
          <span id="open4">(</span>
          <span id="close4">)</span>: 
          <span class="type">string</span> 
          <span id="open5">{</span>
          <span class="statement">return</span> 
          <span class="stringliteral">"Hello "</span> + 
          <span class="keyword">this</span>.
          <span class="identifier">name</span>;
          <span id="close5">}</span>
        </span>
        <span id="close1">}</span>
      </span>
    </pre>
</body>

我正在尝试选择:,;,=和+符号,但我不知道怎么做。(一个在第8行)
我试着用谷歌搜索,但没有一个答案起作用。

tkclm6bt

tkclm6bt1#

以下解决方案适用于6个css类和一些分层特性
https://jsfiddle.net/tadpole/pthgxqny/1/

.classDecl{
  color: red;
}
.classDecl > span{
  color: black;
}
span.ctorDecl{
  color: red;
}
span.ctorDecl > span{
  color: black;
}
span.methodDecl{
  color: red;
}
span.methodDecl > span{
  color: black;
}

相关问题