jQuery-根据< a>的内容应用类 [英] Jquery - Apply class based on contents of <a>

查看:95
本文介绍了jQuery-根据< a>的内容应用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平导航菜单,可以编辑其源代码,我唯一的选择是在通过JQUERY加载时动态添加类.

I have a horizontal navigation menu which I can edit the source of, my only option is to add classes dynamically when it loads through JQUERY.

假设我有3个标签:

Home, Profile, Blog

每个都有这样的链接:

<a href="home.html">Home</a>

JQUERY是否有可能在<a> </a>标记之间查找并找到文本(例如Home)并将该文本用作类?

Is it possible for JQUERY to look between the <a> </a> tags and find the text (E.g. Home) and use that text as the class?

所以我的<a href="home.html">变为:<a class="Home" href="home.html">

推荐答案

$("a").each(function () {
  var self = $(this);
  self.addClass(self.text());
});

这篇关于jQuery-根据&lt; a&gt;的内容应用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆