Is it possible to refer to a <li>
index? I want to do that to be able to add/remove/reorder the <li>
tags without having to manually change the reference number.
Here is an example with some made-up HTML that outlines how I think doing that could work:
<ol>
<li>...</li>
<li ref="some-ref">Some text that I want to refer to later ...</li>
<li>...</li>
<li>According to <liref ref="some-ref" />, ...</li>
</ol>
The desired output would of course be:
Is there something like this in real-life HTML?
How do I read or interpret an index? An index is a tool that simplifies the measurement of movements in a numerical series. Most of the specific CPI indexes have a 1982-84 reference base. That is, BLS sets the average index level (representing the average price level)-for the 36-month period covering the years 1982, 1983, and 1984-equal to 100.
"Indices" is used when referring to mathematical, scientific and statistical contexts. It is used to refer to a numbers, symbols, and figures comparing a value to a standard. "Indexes" is usually used in reference to written documents, such as bibliographical or citation listings.
An index is a list of all the names, subjects and ideas in a piece of written work, designed to help readers quickly find where they are discussed in the text. Usually found at the end of the text, an index doesn’t just list the content (that’s what a table of contents is for), it analyses it. Where are indexes used?
An index provides a map to a report’s content. It does this through identifying key themes and ideas, grouping similar concepts, cross-referencing information and using clear formatting. A good index will: cross-reference information to point to other headings of interest or preferred terms. For example, a back-of-the-book index might read:
HTML doesn't provide such mechanism. You need server-side or client-side scripting to achieve this.
You can also try using CSS counter
properties:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters
For what it's worth, I have accomplished what I wanted using jQuery. It doesn't answer the question directly, but can be useful to someone. It works for nested lists too.
Take this example:
<ol>
<li>...</li>
<li>Nested list
<ol>
<li>...</li>
<li id="ref1">Some text that I want to refer to later ...</li>
<li>...</li>
<li id="ref2">Different text that I want to refer to ...</li>
<li>According to <span class="ref" name="#ref2"></span>, ...</li>
</ol>
</li>
<li>According to <span class="ref" name="#ref1"></span>, ...</li>
</ol>
Using this jQuery code:
$(function() {
$('.ref').each(function() {
var refLi = $($(this).attr('name'));
var refLiAncestorLis = refLi.parents('li');
var reference = '';
for (var i = refLiAncestorLis.length - 1; i >= 0; i--) {
reference += $(refLiAncestorLis[i]).index() + 1 + '.';
}
reference += refLi.index() + 1 + '.';
$(this).text(reference);
});
});
You can get this output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With