javascript - Javascript to highlight current page
问题描述
New javascript user here... thanks in advance for any help. I am adding pagination to a news site so only 10 articles show on page 1 and you have to select 2, 3, 4, etc. to see more articles. The code I have allows me to go to page 2, 3, etc., but it is not highlighting my active page for some reason.
Here is the code I have:
HTML:
<table class="news-pages" id="paginator">
<tr>
<% for(var i = 1; i <= num_pages; i++) { %>
<td class="news-pages-option"><%-i%></td>
<% } %>
</tr>
</table>
JS:
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
const page = getParameterByName('p');
$('.news-pages-option').parent().addClass('active');
CSS:
.news-pages-option.active {
background-color: #cccccc;
border: none;
padding: 3px 8px 3px 8px;
text-decoration: underline;
}
解决方案
看起来您想突出显示与从 url 中提取的页面变量相对应的选项卡。尝试:
const page = getParameterByName('p') - 1; // the array of tabs is zero-based so subtract one
$('.news-pages-option:eq(' + page + ')').addClass('active');
推荐阅读
- javascript - 解析时位置 608 处的 JSON 中的意外字符串
- python - 如何在选定的子列表索引处提取、访问和使用列表的所有值?
- typescript - 如果未包含在当前项目中,则忽略引用 typescript 库的类型
- ios - 是否可以在 dart/flutter 项目的编译时获取当前时间?
- java - onClickListeners 之间的变量重置为默认值
- python - 是否可以使用“LIKE”和 SqlAlchemy 在 JSONB 列表中搜索值?
- php - 带if语句的php代码说明
- python - Pytorch 和 numpy 广播规则的区别?
- regex - 如果输入的前 3 个字符的值为“xyz”,如何禁用提交按钮并显示 div
- c# - 如何在 ListView 中的 ViewCell 中添加 Material Design 波纹效果?