如何使用 jQuery 添加和删除链接上的类?

发布于 2024-08-17 10:15:37 字数 1333 浏览 6 评论 0原文

让我更好地描述我想要在这里做什么。我使用的是 Wikispaces,默认情况下,当用户向侧面导航栏添加新页面链接时,wikispaces 会插入两个类之一:wiki_linkwiki_link_new。它看起来像这样...

<a href="/Map+Page" class="wiki_link">Map Page</a><br/>
<a href="/Staff+Olympics" class="wiki_link">Staff Olympics</a><br/>
<a href="/Staff+Meetings" class="wiki_link_new">Staff Meetings</a><br/>

我想自动删除这两个类(wiki_link 和 wiki_link_new),并向链接添加一个类以更好地表示页面 URL。 因此,对于 /Map+页面 url,将在链接中添加一个类,该类是链接标题的前 3 个字母...地图。然后,如果链接 url 有两个单词(如 /Map+Page),该类还将在 url 中添加第二个单词的前 3 个字母,前面加一个破折号。因此,对于 /Map+Page,要添加到此链接的类将是

class="map-pag"

或者,如果它是 /Staff+Olympics url,要添加的类将是...

class="sta-oly"

我猜如果有超过 2 个单词在 a:link url 中,在将 CSS 类应用于链接时,多余的单词将被忽略,只关注第一个单词的前 3 个字母,后面是破折号,然后是前 3 个字母第二个词的。

最重要的是,我想删除
的所有实例,这些实例也是维基空间默认插入的。

所以理想情况下,我想要一个 jQuery 解决方案,将上面的 3 a:links 更改为这个...

<a href="/Map+Page" class="map-pag">Map Page</a>
<a href="/Staff+Olympics" class="sta-oly">Staff Olympics</a>
<a href="/Staff+Meetings" class="sta-mee">Staff Meetings</a>

不确定我的类命名约定是否是最好的。所以如果您有更好的建议,我很乐意听到! :)

感谢您提供的任何帮助!

Let me give a better description of exactly what I'm wanting to do here. I'm using Wikispaces, and by default, when a user adds a new page link to the side navigation bar, wikispaces inserts either one of two classes: wiki_link or wiki_link_new. Here's what it looks like...

<a href="/Map+Page" class="wiki_link">Map Page</a><br/>
<a href="/Staff+Olympics" class="wiki_link">Staff Olympics</a><br/>
<a href="/Staff+Meetings" class="wiki_link_new">Staff Meetings</a><br/>

I'd like to automatically remove both classes (wiki_link and wiki_link_new), and add a class to the link that better represents the page url. So, for /Map+Page url, a class would be added to the link that is the first 3 letters of the link title... map. And then, if the link url has two words (as in /Map+Page), the class would also add the first 3 letters of the second word in the url, preceded by a dash. So, for /Map+Page, the class to be added to this link would be

class="map-pag"

Or, if it was the /Staff+Olympics url, the class to be added would be...

class="sta-oly"

And I guess if there were more than 2 words within the a:link url, the excess words would be ignored in regards to applying a CSS class to the link, only paying attention to the first 3 letters of the first word, followed by a dash, and then followed by the first 3 letters of the second word.

And on top of all of that, I'd like to remove all instances of <br/>, which are also inserted by default by wikispaces.

So ideally, I'd like a jQuery solution that would change the 3 a:links above to this...

<a href="/Map+Page" class="map-pag">Map Page</a>
<a href="/Staff+Olympics" class="sta-oly">Staff Olympics</a>
<a href="/Staff+Meetings" class="sta-mee">Staff Meetings</a>

Not sure if my class naming conventions are the best. So if you have a better suggestion, I'd love to hear it! :)

Thanks for any help you can provide!

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

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

发布评论

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

评论(1

◇流星雨 2024-08-24 10:15:37

这应该做你想要的:

// Select the links, and remove their classes
$(".wiki_link, .wiki_link_new").attr('class','').each(function(){
    var className = $(this).attr('href').substr(1).toLowerCase().split('+').slice(0,2).join('-');
    $(this).addClass(className);
});

我只是用 CSS 隐藏
标签。操作 DOM 需要花费大量时间和资源,因此如果您可以隐藏它们,速度会更快。

CSS

#container br { display: none; }

其中#container 是这些a 标记的父元素。

如果您确实想使用 jQuery:

jQuery

$("#container br").remove();

同样,其中 #containera 标记列表的父级。

This should do what you want:

// Select the links, and remove their classes
$(".wiki_link, .wiki_link_new").attr('class','').each(function(){
    var className = $(this).attr('href').substr(1).toLowerCase().split('+').slice(0,2).join('-');
    $(this).addClass(className);
});

I would just hide the <br /> tags with CSS. Manipulating the DOM is expensive in time and resources, so if you can just hide them it would be faster.

CSS

#container br { display: none; }

Where #container is whatever element is the parent of those a tags.

If you really want to use jQuery:

jQuery

$("#container br").remove();

Again, where #container is the parent of the list of a tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文