2013-04-08 10 views
7

Ich habe enthält einige dynamisch bevölkerten Links in einer Liste auf meiner Website, die auf Dateien verweisen. Ist es möglich, jQuery zu verwenden, um zu sehen, ob der Name der Datei mit .pdf endet und eine Klasse zum href oder ähnlichem hinzufügt, wenn der Linktext mit .mp3 endet?jQuery Klasse in dem href wenn Link bestimmten Text

Zum Beispiel habe ich die folgenden Links in meiner Liste:

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

würde ich wie das Ende Buchstaben zu erkennen und zu verschiedenen Klassen zu den Links hinzufügen, also auf den Link, der den Text Document1.pdf hat ich die Klasse hinzufügen würde pdf auf das Ankerelement und den Link mit dem Text song1.mp3 würde ich die Klasse mp3 auf das Ankerelement hinzufügen.

Antwort

35

die Attributselektor Verwendung:

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

Optionen:

 
    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 für weitere Informationen.

-1
$('a[href$=".mp3"]').addClass("mp3"); 
$('a[href$=".pdf"]').addClass("pdf"); 
+0

Erm, vielleicht die zweite – Eoin

+0

'$ sagen sollte ('a [href $ = ". pdf"] '). addClass ("pdf"); ' – Eoin

+0

Bearbeitet, danke. – Aioros

1

gegeben Ihr alle diese Links haben Klasse .file

var exts = ['pdf','xls']; 
$('a.file').each(function(){ 
    if($(this).attr('href').match(new RegExp('('+exts.join('|')+')'), 'gi')) 
     $(this).addClass($(this).attr('href').match(/\w{3}$/gi)[0]); 
}) 

Dieser Code wird die Erweiterung Klasse Für alle diese Links hinzufügen, die Dateierweiterung in exts Array.

0

Anstatt schwer, alle Arten Codierung, können Sie auch eine Lösung machen, die dies automatisch für alle Ihre Links:

var regex = "/\..{3,4}$/"; 
$('a').each(function() { 
    $(this).addClass(regex.match($(this).attr("href"))[0] 
}); 
Verwandte Themen