
在前端开发中,我们经常需要根据业务逻辑或用户交互动态地修改网页内容。其中,操作列表(<ul>或<ol>)中的列表项(<li>)是常见的需求,例如调整它们的顺序或更新其内部链接。本教程将深入探讨如何使用纯javascript实现这一目标,即使在元素没有唯一id的情况下也能高效完成。
我们将以一个典型的标签列表为例,演示如何将列表中的第一个<li>元素移动到第10个位置,并将其内部链接的href属性从"collections/all"修改为"collections/all/cotton"。
假设我们有如下的HTML列表结构:
<ul class="tags tags--collection inline-list"> <li><a href="collections/all">All</a></li> <li class="tag--active"><a href="/collections/all" title="Remove tag accessories">accessories</a></li> <li><a href="collections/all/athletic" title="Show products matching tag athletic">athletic</a></li> <li><a href="collections/all/bracelet" title="Show products matching tag bracelet">bracelet</a></li> <li><a href="collections/all/coat" title="Show products matching tag Coat">Coat</a></li> <li><a href="collections/all/cold" title="Show products matching tag cold">cold</a></li> <li><a href="collections/all/cotton" title="Show products matching tag cotton">cotton</a></li> <li><a href="collections/all/fall" title="Show products matching tag Fall">Fall</a></li> <li><a href="collections/all/gloves" title="Show products matching tag gloves">gloves</a></li> <li><a href="collections/all/leather" title="Show products matching tag leather">leather</a></li> <li><a href="collections/all/rain" title="Show products matching tag Rain">Rain</a></li> <li><a href="collections/all/raincoat" title="Show products matching tag Raincoat">Raincoat</a></li> <li><a href="collections/all/scarf" title="Show products matching tag Scarf">Scarf</a></li> <li><a href="collections/all/silk" title="Show products matching tag silk">silk</a></li> <li><a href="collections/all/summer" title="Show products matching tag summer">summer</a></li> <li><a href="collections/all/vintage" title="Show products matching tag vintage">vintage</a></li> <li><a href="collections/all/watch" title="Show products matching tag watch">watch</a> </li> <li><a href="collections/all/winter" title="Show products matching tag winter">winter</a> </li> </ul>
我们将通过以下JavaScript代码实现上述功能:
选择目标无序列表(<ul>) 首先,我们需要获取到包含所有列表项的父元素。由于没有ID,我们可以利用其类名进行选择。
const list = document.querySelector('ul.tags.tags--collection.inline-list');这里使用了 document.querySelector 方法,它接受一个CSS选择器作为参数,并返回文档中匹配该选择器的第一个元素。
立即学习“Java免费学习笔记(深入)”;
选择要移动的列表项(第一个<li>) 接下来,我们需要定位到要移动的第一个<li>元素。
const first = list.querySelector(':scope > li:nth-child(1)');选择目标位置的列表项(第十个<li>) 然后,我们选择第一个<li>要插入到其后面的目标位置元素,即当前的第十个<li>。
const tenth = list.querySelector(':scope > li:nth-child(10)');与上一步类似,使用 :nth-child(10) 来定位第十个列表项。
移动列表项 现在,我们将第一个<li>移动到第十个<li>之后。
tenth.insertAdjacentElement('afterend', first);修改链接的href属性 最后,我们需要修改刚刚移动的<li>元素内部<a>标签的href属性。
first.querySelector(':scope > a').href = 'collections/all/cotton';将上述步骤整合,得到完整的JavaScript代码如下:
// 选择无序列表,通过其类名进行定位
const list = document.querySelector('ul.tags.tags--collection.inline-list');
// 选择列表中第一个<li>元素
// :scope 确保选择器只在 list 元素内部生效
const first = list.querySelector(':scope > li:nth-child(1)');
// 选择列表中第十个<li>元素,作为目标插入位置的参照
const tenth = list.querySelector(':scope > li:nth-child(10)');
// 将第一个<li>元素移动到第十个<li>元素之后。
// insertAdjacentElement 方法会将元素从原位置移除并插入到新位置。
// 这会使得原先的第十个元素变为第九个,而第一个元素则占据了第十个位置。
tenth.insertAdjacentElement('afterend', first);
// 选择移动后的第一个<li>元素内部的<a>标签,并修改其href属性
first.querySelector(':scope > a').href = 'collections/all/cotton';通过本教程,我们学习了如何利用JavaScript的 querySelector 和 insertAdjacentElement 等DOM操作方法,在没有ID的情况下,灵活地控制HTML列表项的顺序和属性。掌握这些技巧将使您能够创建更具交互性和动态性的Web页面。
以上就是使用JavaScript动态调整列表项位置与链接属性的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号