如何仅使用 jQuery 创建维基百科的脚注突出显示
我想复制维基百科的脚注突出显示,仅在 jQuery 和 CSS 类中单击文本引用。 我找到了一个网页,描述了如何使用 CSS3 执行此操作,然后IE 的 JavaScript 解决方案。然而,我想仅使用 jQuery 来实现这一点,因为我正在执行此操作的网站已经有一堆 jQuery 元素。
我已经列出了该过程的清单。
- 单击文本内引文
- 将已突出显示的脚注的
标记上的突出显示类替换为标准脚注类。
- 向适当的脚注添加突出显示
- 使用 jQuery 将页面向下滚动到适当的脚注。
到目前为止,我已经想出了一些 jQuery,但我对它非常陌生,到目前为止,我严重依赖教程和插件。这是我用一些简单的英语来表达我还没有弄清楚的部分。
$('.inlineCite').click(function() {
$('.footnoteHighlight').removeClass('footnoteHighlight').addClass('footnote');
//add highlight to id of highlightScroll
//scroll to footnote with matching id
});
如果我有一种方法将选择器的一部分传递到函数中并将其转换为变量,我相信我可以实现它。我确信你们中的一位大师很可能会想出一些东西,让我认为我所做的一切都感到羞愧。任何帮助将不胜感激,所以提前感谢您。
干杯。
I would like to duplicate Wikipedia's footnote highlighting from in-text citation click solely in jQuery and CSS classes. I found a webpage that describes how to do so with CSS3 and then a JavaScript solution for IE. I would like however to do so solely with jQuery as the site I'm doing it on already has a bunch of jQuery elements.
I've come up with a list of the process.
- In-Text Citation Clicked
- Replace highlight class with standard footnote class on
<p>
tag of footnotes that are already highlighted. - Add highlight to the appropriate footnote
- Use jQuery to scroll down the page to appropriate footnote.
I've come up with some jQuery so far but I'm extremely new to it relying heavy on tutorials and plugins to this point. Here is what I have with some plain English for the parts I haven't figured out yet.
$('.inlineCite').click(function() {
$('.footnoteHighlight').removeClass('footnoteHighlight').addClass('footnote');
//add highlight to id of highlightScroll
//scroll to footnote with matching id
});
If I had a method to pass a part of the selector into the function and turn it into a variable I believe I could pull it off. Most likely I'm sure one of you gurus will whip something up that puts anything I think I have done to shame. Any help will be greatly appreciated so thank you in advance.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我逐字复制了维基百科的上标标记:
并使用了以下 jQuery:
关键是从单击的链接中获取
href
,然后使用它来选择元素并应用突出显示类。此外,您不需要以编程方式向下滚动页面,因为浏览器会自动为您处理此操作。
这是一个完整的工作示例: http://jsfiddle.net/andrewwhitaker/dkWJm/2/
更新:我在下面的评论中注意到您想要设置滚动动画,具体操作方法如下:
注意:
event.preventDefault()
),这将跳转到id
与单击的链接的href
匹配的元素。animate
平滑滚动html
元素下降到适当的位置,这是使用offset()
确定的目标元素。duration
参数(我指定为 3000)是您可以调整以延长/缩短滚动持续时间的参数。这是一个示例: http://jsfiddle.net/andrewwhitaker/dkWJm/4/ (已更新以在 IE8/Chrome/FF 3 中工作)
I copied Wikipedia's superscript markup verbatim:
And used the following jQuery:
The key is grabbing the
href
off of the link that was clicked, and then using that to select the element and apply the highlight class.Also, you don't need to scroll the page down programmatically, because the browser handles this for you automatically.
Here's a complete working example: http://jsfiddle.net/andrewwhitaker/dkWJm/2/
Update: I noticed in your comment below you'd like to animate the scrolling, here's how you would do that:
Notes:
event.preventDefault()
), which would be to jump to the element with theid
matching the clicked link'shref
.animate
to smoothly scroll thehtml
element down to the appropriate position, which is determined usingoffset()
on the target element.duration
argument (I specified 3000) is what you would tweak to lengthen/shorten the duration of the scroll.Here's an example of this: http://jsfiddle.net/andrewwhitaker/dkWJm/4/ (Updated to work in IE8/Chrome/FF 3)
这里有一个更好的主意 - 使用
#hash
链接进行链接,然后使用 jQuery 增强它。在 HTML 中:请注意,无论启用或不启用 JavaScript,滚动都可以正常工作,并且如果用户选择通过添加书签或将其复制到其他位置来保存链接,您的页面仍将按预期工作。
现在我们定义一个简单的CSS类来突出显示选定的脚注:
并且我们应用一些JavaScript逻辑:
如果您想检查用户是否导航到
#hash
URL,您可以使用location .hash
来做到这一点:Here's a better idea - use
#hash
links to do the linking, then enhance it with a jQuery. In your HTML:Notice that the scrolling will work with or without JavaScript enabled, and if the user chooses to save the link by bookmarking it or copying it to somewhere else your page will still work as expected.
Now we define a simple CSS class to highlight the selected footnote:
And we apply some JavaScript logic:
If you want to check whether the user is navigating to a
#hash
URL, you can uselocation.hash
to do it:为此不再需要使用 jQuery; CSS3
:target
伪类现在以纯 CSS 方式执行此操作。Codepen 链接
参考:如何使用 CSS3 动态突出显示维基百科等内容
It is no longer necessary to use jQuery for this; the CSS3
:target
pseudo-class now does this in a pure-CSS way.Codepen link
Reference: How to Dynamically Highlight Content Like Wikipedia Using CSS3
也许更好的方法是打开包含参考详细信息的工具提示,而不是上下跳跃。简单而简洁的插件位于: http://livereference.org 除了读取用户定义的参考之外,它还可以自动检索出版物详细信息来自给定的 ISBN 或 PubMed ID。
Perhaps the better approach would be opening a tooltip with the reference details instead of jumping up and down. Simple and neat plugin is at: http://livereference.org In addition to reading user defined reference it can automatically retrieve publication details from a given ISBN or PubMed ID.