Files
mintlify[bot] 90131de012 docs: translate className and Tailwind updates into es, fr, zh (#7306)
Co-authored-by: mintlify[bot] <109931778+mintlify[bot]@users.noreply.github.com>
2026-09-09 00:04:33 +00:00

111 lines
2.6 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "代码分组"
description: "使用 CodeGroup 组件在标签页界面中展示多个代码示例,让读者可以比较不同编程语言的实现方式。"
keywords: ["code groups", "tabbed code", "multi-language examples", "CodeGroup"]
---
使用 `CodeGroup` 组件在带有标签页的界面中展示多个代码块,便于用户比较不同编程语言的实现,或查看完成同一任务的替代方案。
<CodeGroup>
```javascript helloWorld.js
console.log("Hello World");
```
```python hello_world.py
print('Hello World!')
```
```java HelloWorld.java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
</CodeGroup>
代码分组将从你的 `docs.json` 文件继承全局样式。可通过 `styling.codeblocks` 自定义主题。配置选项参见 [Settings](/zh/organize/settings-appearance#styling)。
当标签相同时,同一页面上的代码分组会与其他代码分组以及 [Tabs](/zh/components/tabs) 自动同步。当你在某个代码分组中选择一种语言时,所有具有相同标签的 Tabs 和代码分组都会更新为显示相同的语言。
<div id="creating-code-groups">
## 创建代码组
</div>
要创建代码组,请使用 `<CodeGroup>` 标签包裹多个代码块。每个代码块必须包含一个 title其值将作为标签页的标签。
````mdx
<CodeGroup>
```javascript helloWorld.js
console.log("Hello World");
```
```python hello_world.py
print('Hello World!')
```
```java HelloWorld.java
class HelloWorld {
public static void main(String[] args) {
System.out.println("你好,世界!");
}
}
```
</CodeGroup>
````
<div id="language-dropdown">
## 语言下拉菜单
</div>
你可以通过使用 `dropdown` 属性,将 CodeGroup 中的选项卡替换为下拉菜单,以在不同语言之间切换。
<CodeGroup dropdown>
```javascript helloWorld.js
console.log("Hello World");
```
```python hello_world.py
print('Hello World!')
```
```java HelloWorld.java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
</CodeGroup>
````mdx highlight=1
<CodeGroup dropdown>
```javascript helloWorld.js
console.log("你好世界");
```
```python hello_world.py
print('你好世界!')
```
```java HelloWorld.java
class HelloWorld {
public static void main(String[] args) {
System.out.println("你好,世界!");
}
}
```
</CodeGroup>
````
<div id="properties">
## 属性
</div>
<ResponseField name="className" type="string">
应用于代码组容器的额外 CSS 类。
</ResponseField>