I am writing a small application to display c++ code in the browser and using Google code-prettify for it. As a requirement, I have to scroll to a particular line number. After some searching, I found to select node, I can use the following snippet
#cd li:nth-child(<line>) {
outline: 1px solid #f00;
}
What could be the way, to scroll to a particular line number?
I tried using the following code for the same, but position()
is returning undefined
$('#cd').scrollTop($('#cd li:nth-child(<line>)').position().top)
<html>
<head>
<style>
.prettyprint ol.linenums>li {
list-style-type: decimal;
}
li.L0,
li.L1,
li.L2,
li.L3,
li.L4,
li.L5,
li.L6,
li.L7,
li.L9,
li.L8 {
background-color: #fdf6e3;
}
</style>
</head>
<body>
<pre class="prettyprint linenums" id="cd">
#ifndef _ABC_HPP</a>
#define _ABC_HPP</a>
#include <myheader.h>
#endif
</pre>
</body>
<script>
$('#cd').scrollTop($('#cd li:nth-child(3)').position().top)
</script>
</html>
I expect on load, the page should scroll to a given line number.