<!DOCTYPE html> <html> <head> <title>Code Display Demo</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/default.min.css"> <style> pre { background-color: #f4f4f4; padding: 10px; } code { font-family: "Courier New", Courier, monospace; } </style> </head> <body> <pre><code class="javascript"> function helloWorld() { console.log("Hello, World!"); } </code></pre> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script> <script> document.addEventListener('DOMContentLoaded', function () { hljs.highlightAll(); }); </script> </body> </html>
时间: 2024-04-14 19:01:59 浏览: 111
你提供的代码看起来是一个完整的HTML示例,它使用了highlight.js库来高亮显示JavaScript代码。当你在浏览器中打开这个HTML文件时,你将看到一个带有高亮代码的页面。
在这个示例中,我们引入了highlight.js库的默认样式表,并在`<style>`标签中定义了一些自定义的样式。我们使用`<pre>`和`<code>`标签来包裹需要展示的代码,并为它们应用了一些样式。
在页面加载完成后,我们使用`document.addEventListener`来监听`DOMContentLoaded`事件,并在事件触发时调用`hljs.highlightAll()`来触发代码的高亮显示。
这个示例中的JavaScript代码是一个简单的函数`helloWorld()`,它会在控制台输出"Hello, World!"。
希望这个完整的示例对你有所帮助!如果你有任何问题,请随时向我提问。
相关问题
前端如何展示代码,并保留代码格式,请帮我写一个完整的demo
当前端需要展示代码并保留代码格式时,可以使用 `<pre>` 和 `<code>` 标签来包裹代码,并使用 CSS 样式来进行格式化。下面是一个完整的示例代码:
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<title>Code Display Demo</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<pre><code class="javascript">
function helloWorld() {
console.log("Hello, World!");
}
</code></pre>
<script src="highlight.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
hljs.highlightAll();
});
</script>
</body>
</html>
```
CSS (styles.css):
```css
pre {
background-color: #f4f4f4;
padding: 10px;
}
code {
font-family: "Courier New", Courier, monospace;
}
```
在上述示例中,我们使用了 `<pre>` 和 `<code>` 标签来包裹需要展示的代码,并为 `<pre>` 添加了一些样式来设置背景色和内边距。我们还为 `<code>` 设置了字体样式,以保证代码的等宽字体显示。
我们引入了 `highlight.js` 库,并在页面加载完成后调用 `hljs.highlightAll()` 来触发代码高亮。
你可以将上述代码保存为 HTML 文件,并将 `highlight.js` 和 `styles.css` 文件放置在相同目录下。然后在浏览器中打开该 HTML 文件,你将看到带有高亮代码的页面。
这个示例使用了 `highlight.js` 库来进行代码高亮,你可以根据需要自定义语言类型和样式。希望这个完整的示例对你有帮助!
用html语言制作一个包含grid intro form table carousel button jumbotron card collapse modal shape media的网页
抱歉,作为一名AI语言模型,我无法展示html语言制作的网页。但我可以提供一个示例代码,您可以在本地编写并在浏览器中打开查看效果。
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+J7jWPuZTlXOMX3EfocM5p5Jw6IkkB" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<!-- Grid -->
<h1>Grid Example</h1>
<div class="row">
<div class="col-md-6 bg-primary text-white">Column 1</div>
<div class="col-md-6 bg-secondary text-white">Column 2</div>
</div>
<!-- Intro -->
<h1>Intro Example</h1>
<p>This is an example of an intro paragraph.</p>
<!-- Form -->
<h1>Form Example</h1>
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<!-- Table -->
<h1>Table Example</h1>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
<!-- Carousel -->
<h1>Carousel Example</h1>
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img src="https://via.placeholder.com/800x400?text=Slide+1" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="https://via.placeholder.com/800x400?text=Slide+2" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="https://via.placeholder.com/800x400?text=Slide+3" class="d-block w-100" alt="...">
</div>
</div>
</div>
<!-- Button -->
<h1>Button Example</h1>
<button type="button" class="btn btn-primary">Primary</button>
<button type="button" class="btn btn-secondary">Secondary</button>
<!-- Jumbotron -->
<h1>Jumbotron Example</h1>
<div class="jumbotron">
<h1 class="display-4">Hello, world!</h1>
<p class="lead">This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
<hr class="my-4">
<p>It uses utility classes for typography and spacing to space content out within the larger container.</p>
<a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>
</div>
<!-- Card -->
<h1>Card Example</h1>
<div class="card" style="width: 18rem;">
<img src="https://via.placeholder.com/286x180?text=Card+Image" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card Title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
<!-- Collapse -->
<h1>Collapse Example</h1>
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</button>
</h2>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h2 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Collapsible Group Item #2
</button>
</h2>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div class="card-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
</div>
</div>
</div>
<!-- Modal -->
<h1>Modal Example</h1>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- Shape -->
<h1>Shape Example</h1>
<div class="shape-example"></div>
<!-- Media -->
<h1>Media Example</h1>
<div class="media">
<img src="https://via.placeholder.com/64x64?text=Image" class="mr-3" alt="...">
<div class="media-body">
<h5 class="mt-0">Media heading</h5>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
</div>
</div>
</div>
</div>
</div>
<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+J7jWPuZTlXOMX3EfocM5p5Jw6IkkB" crossorigin="anonymous"></script>
</body>
</html>
```
该代码使用了Bootstrap框架,包含了您所提到的所有组件,您可以根据需要进行修改和调整。
阅读全文