如果链接包含特定文本,jQuery 将类添加到 href [英] jQuery add class to href if link contains specific text

查看:34
本文介绍了如果链接包含特定文本,jQuery 将类添加到 href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上的列表中有一些动态填充的链接,这些链接指向文件.是否可以使用 jQuery 查看文件名是否以 .pdf 结尾,并在链接文本以 .mp3 结尾的情况下向 href 或类似内容添加一个类?

例如,我的列表中有以下链接:

  • Document1.pdf
  • Song1.mp3
  • Song2.m4a
  • Document2.doc

我想检测结尾字母并向链接添加不同的类,因此对于具有文本 Document1.pdf 的链接,我将类 pdf 添加到锚元素,并且带有文本 Song1.mp3 的链接我将类 mp3 添加到锚元素.

解决方案

使用属性选择器:

$('a[href$=".mp3"]')...

选项:

<前>属性包含前缀选择器 [name|="value"]选择具有带值的指定属性的元素要么等于给定的字符串,要么以该字符串开头,后跟由连字符 (-) 组成.属性包含选择器 [name*="value"]选择具有指定属性的元素包含给定子字符串的值.属性包含单词选择器 [name~="value"]选择具有带值的指定属性的元素包含给定的单词,由空格分隔.属性以选择器结尾 [name$="value"]选择具有指定属性的元素值完全以给定的字符串结尾.比较区分大小写.属性等于选择器 [name="value"]选择具有指定属性的元素value 正好等于某个值.属性不相等选择器 [name!="value"]选择不具有指定属性的元素,或者确实具有指定的属性但没有特定的值.属性以选择器 [name^="value"] 开头选择具有指定属性的元素值完全以给定的字符串开头.有属性选择器[名称]选择具有指定属性的元素,具有任何值.多属性选择器 [name="value"][name2="value2"]匹配与所有指定属性过滤器相匹配的元素.

查看 API 以获取更多信息.

I have some dynamically populated links in a list on my site that link to files. Is it possible to use jQuery to see if the name of the file ends with .pdf and add a class to the href or similar if the link text ends with .mp3?

For example I have the following links in my list:

  • Document1.pdf
  • Song1.mp3
  • Song2.m4a
  • Document2.doc

I would like to detect the end letters and add different classes to the links, so to the link which has the text Document1.pdf I'd add the class pdf to the anchor element, and the link with the text Song1.mp3 I'd add the class mp3 to the anchor element.

解决方案

Use the attribute selector:

$('a[href$=".mp3"]')...

Options:

    Attribute Contains Prefix Selector [name|="value"]
    Selects elements that have the specified attribute with a value 
    either equal to a given string or starting with that string followed 
    by a hyphen (-).

    Attribute Contains Selector [name*="value"]
    Selects elements that have the specified attribute with a 
    value containing the a given substring.

    Attribute Contains Word Selector [name~="value"]
    Selects elements that have the specified attribute with a value
    containing a given word, delimited by spaces.

    Attribute Ends With Selector [name$="value"]
    Selects elements that have the specified attribute with a 
    value ending exactly with a given string. The comparison is case sensitive.

    Attribute Equals Selector [name="value"]
    Selects elements that have the specified attribute with a 
    value exactly equal to a certain value.

    Attribute Not Equal Selector [name!="value"]
    Select elements that either don’t have the specified attribute, 
    or do have the specified attribute but not with a certain value.

    Attribute Starts With Selector [name^="value"]
    Selects elements that have the specified attribute with a 
    value beginning exactly with a given string.

    Has Attribute Selector [name]
    Selects elements that have the specified attribute, with any value.

    Multiple Attribute Selector [name="value"][name2="value2"]
    Matches elements that match all of the specified attribute filters.

Check out the API for additional information.

这篇关于如果链接包含特定文本,jQuery 将类添加到 href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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