JQuery 添加/删除链接
我有一个链接列表,用于过滤结果,例如
过滤器 1、过滤器 2、过滤器 3、过滤器无
当单击过滤器链接时,我使用 JQuery 加载命令更新 div 的内容。
然后我想要发生的是,用户单击的过滤器链接变成只是文本而不是链接(这将阻止用户重新单击链接,并显示他们正在使用哪个过滤器)。如果用户然后单击另一个过滤器链接,我希望恢复之前的链接,然后删除他们单击的过滤器上的链接。
我将如何使用 JQuery 来做到这一点?我发现命令删除,但我认为这没有帮助,因为当用户单击不同的过滤器时我将无法恢复它。
谢谢
I have a list of links which I use to filter results e.g.
filter 1, filter 2, filter 3, filter none
When a filter link is clicked I update the contents of a div using the JQuery load command.
What I then want to happen is that the filter link the user clicked on becomes just text rather than a link (this will stop the user re-clicking on the link and also shows them which filter they are using). If the user then clicks on another filter link I want the previous link to be restored and then link on the filter they clicked on removed.
How would I do this usig JQuery? I found the command remove but I don't think this would help as I would not be able to restore it when the user clicks on a different filter.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您应该在这里找到答案 Disabling links with JQuery
感谢 @Will 提供的这个优雅的解决方案。
You should find you answer here Disabling links with JQuery
Thanks @Will for this elegant solution.
例如,与添加类相比,绑定和取消绑定需要大量开销。
绑定/取消绑定解决方案可以工作,但不是最佳解决方案。
这是一个更好的解决方案。
Bind and unbind require a lot of overhead compared to adding a class, for example.
Bind/unbind solutions will work, but are not the best solution.
Here's a better solution.
为了使其成为最简单、最干净的方法,您可以做的是定义两个 css 类,activefilter 和 inactivefilter,第一个使它看起来像一个链接,第二个使它看起来不活动,您的 HTML/jQuery 将是这样的喜欢:
What you could do to make this the most easy and clean way possible is to define two css classes, activefilter and inactivefilter, on the first make it look like a link and on the second make it look inactive and your HTML/jQuery would be something like:
将两个元素(一个是锚标记,另一个只是普通文本)放在一个范围内以进行识别。然后在两个/所有过滤器上使用 jQuery
toggle()
来循环启用/禁用或显示隐藏锚标记或跨度元素。Put two elements one an anchor tag and other just normal text inside a span to identify. Then use jQuery
toggle()
on both/all the filters to cycle the enable/disable or show hide the anchor tag or span elements.锚点如下:
调用禁用函数
调用启用函数
当你想禁用链接时,只要 onclick 返回 false
当你想启用它时
Here's the anchor:
Call the disable function
Call the enable function
When you want to disable the link, just have onclick return false
When you want to enable it
jQuery:
HTML:
Jquery:
HTML:
我知道这有点晚了,但我发现使用 a 标签的一个问题是,除非您拿走指针光标,否则您仍然会得到指针。即使您删除了点击绑定。所以还应该做的一件事是:
cursor:text
I know this is a bit late, but one thing I see an issue with using an a tag is that unless you take away the pointer cursor, you will still get the pointer. Even if you remove the binding for clicking. So one thing that should also be done is:
cursor:text
一个更简单的解决方案是使用 CSS 来指示哪个链接是活动的,而不是将“活动”链接转换为文本。例如,您可以定义一种名为“active”的样式。每次单击链接时,您将:
如果您确实想将链接转换为文本,您可以创建一个如下所示的 html 结构:
然后,每次单击链接时:
Rather than turn the "active" link into text, a simpler solution would be to use CSS to indicate which link is active. For instance, you could define a style called "active". Each time a link is clicked you would:
If you really want to convert a link to text, you might create an html structure like this:
Then, each time a link is clicked you: