html - 使用动态 ID 处理 CSS

标签 html css

我的 html 页面类似于...

<a href="#" id="play_1">play1</a>
<a href="#" id="play_2">play2</a>
<a href="#" id="play_1">play3</a>
<a href="#" id="play_2">play4</a>
<a href="#" id="file_1">file1</a>
<a href="#" id="file_2">file2</a>

我想为 play_"#"和 file_"#"定义通用的 CSS。我如何定义 CSS?

最佳答案

给标签添加类

<a href="#" class="play" id="play_1">play1</a>
<a href="#" class="play" id="play_2">play2</a>
<a href="#" class="play" id="play_1">play3</a> <!-- double ID -->
<a href="#" class="play" id="play_2">play4</a> <!-- double ID -->
<a href="#" class="file" id="file_1">file1</a>
<a href="#" class="file" id="file_2">file2</a>

因为您有两个 ID,所以要么完全删除 ID,要么使它们独一无二

<a href="#" class="play" id="play_1">play1</a>
<a href="#" class="play" id="play_2">play2</a>
<a href="#" class="play" id="play_3">play3</a> 
<a href="#" class="play" id="play_4">play4</a>
<a href="#" class="file" id="file_1">file1</a>
<a href="#" class="file" id="file_2">file2</a>

在css中使用

.play {
  /* add your common play styles here */
}
.file {
  /* add your common file styles here */
}

关于html - 使用动态 ID 处理 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13837431/

相关文章:

javascript - 动态调用Javascript变量

javascript - 如何在这里正确使用 fadeTo 和 fadeIn

html - 当在同一浏览器上加载相同内容时,iexplorer.exe 在远程和本地表现不同

php - 将鼠标悬停在照片上时获取有关照片的详细信息

html - 表格 100% 宽度和高度,以及使用 css 按比例缩放的图像

Java脚本;非常简单的幻灯片

php - 即使代码工作正常也会出现未定义索引错误

html - 如何在列表项周围创建价格标签(如边框)

html - 如何修复交叉 Bootstrap 导航链接?

css - 在原子编辑器中设计交替行