heading anchors
v2.0.2
heading-anchorsWeb組件將錨定鏈接添加到具有ID的標題。
演示|樣式演示|進一步閱讀
< script type =" module " src =" heading-anchors.js " > </ script >
< heading-anchors >
< h2 id =" heading-level-2 " > Heading level 2 </ h2 >
< p > Lorem ipsum dolor sit amet consectetur adipisicing elit. </ p >
< h3 id =" heading-level-3 " > Heading level 3 </ h3 >
< p >
Excepturi eligendi exercitationem, ratione, in delectus vitae veritatis
dolorem porro cupiditate quam eaque voluptates.
</ p >
</ heading-anchors > 此Web組件允許您:
selector屬性附加哪些元素附加錨鏈接position屬性附加錨點鏈接的位置您有一些選擇(選擇其中之一):
npm install @daviddarnes/heading-anchors (也在JSR上的Avaialbe)確保在項目中包括<script> (選擇其中之一):
<!-- Host yourself -->
< script type =" module " src =" heading-anchors.js " > </ script > <!-- 3rd party CDN, not recommended for production use -->
< script
type =" module "
src =" https://www.unpkg.com/@daviddarnes/[email protected]/heading-anchors.js "
> </ script > <!-- 3rd party CDN, not recommended for production use -->
< script
type =" module "
src =" https://esm.sh/@daviddarnes/[email protected] "
> </ script >selector和position默認情況下, heading-anchors將選擇所有h2[id] , h3[id]和h4[id]元素,以將錨固鏈接附加到。但是,對於selector屬性,您可以通過其他選擇器字符串傳遞以選擇:
< heading-anchors selector =" h2[id] " >
<!-- ... -->
</ heading-anchors >注意:建議您使用利用[id]屬性選擇器的選擇器以提高可靠性
默認情況下,使用insertAdjacentHTML API,將在元素之後的錨點鏈接附加heading-anchors 。但是,您可以使用position屬性調整此:
< heading-anchors position =" beforeend " >
<!-- ... -->
</ heading-anchors > 感謝以下人員: