我正在编写一个小应用程序来在浏览器中显示 c++ 代码并使用 Google 代码美化它。作为要求,我必须滚动到特定的行号。经过一番搜索,我发现选择节点,我可以使用以下代码片段
#cd li:nth-child(<line>) {
outline: 1px solid #f00;
}
滚动到特定行号的方法是什么?
我尝试使用以下代码,但position()
正在返回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>
我希望在加载时,页面应该滚动到给定的行号。