javascript - 当 id 已被使用时,如何存储 div 的标识?

标签 javascript jquery html keyboard-shortcuts

所以我正在研究键盘前进和后退脚本,就像他们在 Tumblr 和 9GAG 上使用的那样(您可以使用 J 和 K 键浏览帖子)。

我的方法:

User presses J (next) key

Javascript uses JQuery Viewport to find out which "Jump"-ID is currently in:viewport

Javascript adds +1 to the "Jump"-ID

Javascript gets the position of the increased "Jump"-ID, scrolls there

现在的问题是:

<div class="content jump" id="<?php echo $id; ?>">

我已经定义了 div 的 id。所以它看起来像这样:

<div class="content jump" id="1225">
<div class="content jump" id="1299">
<div class="content jump" id="1206">
<div class="content jump" id="1345">

但是,为了能够以正确的顺序跳转,我需要以某种方式存储额外的标识,可能像这样......

<div class="content jump" id="1225" jump="1">
<div class="content jump" id="1299" jump="2">
<div class="content jump" id="1206" jump="3">
<div class="content jump" id="1345" jump="4">

这只是我的想象。应该有办法解决这个问题。如果你对 J/K 问题有更好的解决方案,也可以告诉我(也许 Tumblr 或 9GAG 有更好的解决方案?)

最佳答案

您可以使用data- 属性。例如:

<div class="content jump" id="1225" data-jump="1">

然后您可以使用

获取该数据
$('#1225').data('jump');

关于javascript - 当 id 已被使用时,如何存储 div 的标识?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11869552/

相关文章:

c# - 如何在 ASP.NET 网站上显示硬盘文件夹中的图像列表?

html - 删除文本单词和标点符号之间的空格

javascript - Polymer:如何将动态值加载到纸质菜单中的纸质项目中?

Javascript:将非常大和非常小的数字转换为固定的人类可读字符串

javascript - 将 HTML 表格转换为 Json 文件

c# - 如何加速具有大量数据的服务器绑定(bind)控件的网页

javascript - 如何在 onTick 中调用 JavaScript 函数

javascript - 忽略最后2条数据的填充

javascript - 如何阻止右浮动 DIV 相互插入,但仅限于特定区域内?

javascript - 表单显示 PHP HTML