简体   繁体   English

突出显示包含在内的HTML /代码部分<pre> <code> tags</code>

[英]Highlighting sections of HTML/code that is wrapped in <pre><code> tags

I am creating an online tutorial and I'm displaying the html code by wrapping it in the <pre><code> tags. 我正在创建一个在线教程,并通过将其包装在<pre><code>标记中来显示html代码。 I would like to be able to highlight certain sections of the code. 我希望能够突出显示代码的某些部分。 Is there a way to do that when the html is wrapped in the the <pre><code> tags? 将html包裹在<pre><code>标记中时,有没有办法做到这一点?

<div data-role="page">
  <h1>Hello</h1>
</div>

I would like to be able to highlight the "page" value of the data-role attribute. 我希望能够突出显示data-role属性的“ page”值。 I tried to surround the "page" code with a span tag and style it, but the span tag showed up in the code. 我尝试用span标签包围“page”代码并设置样式,但是span标签显示在代码中。 I also tried to use &lt; 我还尝试使用&lt; and $gt; $gt; thinking maybe that would escape the < > around the span tags. 想想也许会逃避span标签周围的< > It did, but it showed up in the code. 确实有,但它出现在代码中。

Again, I'm trying to display the code (without screenshots) with certain sections of the code highlighted with yellow. 再次,我试图显示代码(不带屏幕截图),并以黄色突出显示代码的某些部分。

You have to escape everything but the tag. 您必须转义除标记之外的所有内容。 I used <mark> since it seems more semantically correct: 我使用<mark>因为它似乎在语义上更正确:

<pre><code>&lt;div <mark>data-role=&quot;page&quot;</mark>&gt;
  &lt;h1&gt;Hello&lt;/h1&gt;
&lt;/div&gt;</code></pre>

Example http://jsfiddle.net/MFzsS/1/ 示例http://jsfiddle.net/MFzsS/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM