使用 jquery 禁用前一个元素的链接

发布于 2024-12-17 04:01:39 字数 183 浏览 1 评论 0原文

现在我有一个像这样的站点地图:主页>关于我们>我们是谁。我可以使用 jQuery 禁用“关于我们”离开主页的链接仍然处于活动状态吗?另外如果站点地图增加到这个级别首页>服务>咨询>>业务>训练。使用 jQuery 禁用业务、咨询和服务链接,但仍使“主页”链接处于活动状态是否可以实现。

提前致谢

Now I have a sitemap such as this: Home > About Us > Who we Are. Can I use jQuery to disable the link for About Us leaving Home to be still active? Also if the site map increases to this level Home > Services > Consulting > Business > Training. Would it be achievable using jQuery to disable the Business, consulting and Services link, but still making the 'Home' link active.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后咖啡店 2024-12-24 04:01:39

http://jsfiddle.net/HTGBE/2/

<a href="http://jquery.com">Link</a> > 
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> 

和 JS:

$('a:not(:first):not(:last)').click(function(e) {
    e.preventDefault();
});

http://jsfiddle.net/HTGBE/2/

<a href="http://jquery.com">Link</a> > 
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> >
<a href="http://jquery.com">Link</a> 

and the JS:

$('a:not(:first):not(:last)').click(function(e) {
    e.preventDefault();
});
垂暮老矣 2024-12-24 04:01:39

我认为需要更多信息才能为您提供您想要的信息 - 例如您是否试图保持第一个链接处于活动状态?或者文本中带有“Home”的第一个链接?

无论如何,这样的事情听起来就像你所追求的:

// Disable all links except first
$("#sitemap a:gt(0)").removeAttr("href")

// Disable all links except those containing the word 'Home'
$("#sitemap a:not(:contains('Home'))").removeAttr("href");

I think a little more information is needed to give you exactly what you want - for example are you trying to keep the first link active? Or the first link with "Home" in its text?

Regardless, something like this sounds like what you're after:

// Disable all links except first
$("#sitemap a:gt(0)").removeAttr("href")

// Disable all links except those containing the word 'Home'
$("#sitemap a:not(:contains('Home'))").removeAttr("href");
扮仙女 2024-12-24 04:01:39

如果没有看到实际的 HTML,很难准确地说出来,但它应该很容易做到。假设您的 HTML 如下所示:

<div id="sitemap">
    <a href="/home">Home</a> >
    <a href="/home/about">About Us</a> >
    <a href="/home/about/who">Who We Are</a>
</div>

您想要做的是将除第一个锚点之外的所有锚点更改为具有相同内容的跨度。每次页面加载到文档就绪处理程序中时都运行此命令。请注意,您可能需要根据需要添加类/css 以保持正确的样式。

<script type="text/javascript">
   $(function() {
       $('#sitemap a:not(:first)').wrap('<span></span>').unwrap();
   });
</script>

It's hard to say exactly without seeing your actual HTML, but it should be easily doable. Let's say your HTML looks like:

<div id="sitemap">
    <a href="/home">Home</a> >
    <a href="/home/about">About Us</a> >
    <a href="/home/about/who">Who We Are</a>
</div>

What you want to do is change all the anchors except the first to span's with the same content. Run this every time the page loads in a document ready handler. Note that you may need to add classes/css as necessary to maintain the correct styling.

<script type="text/javascript">
   $(function() {
       $('#sitemap a:not(:first)').wrap('<span></span>').unwrap();
   });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文