2013-04-08 30 views
7

Mam kilka dynamicznie wypełnianych linków na mojej stronie z linkiem do plików. Czy można użyć jQuery, aby sprawdzić, czy nazwa pliku kończy się na .pdf i dodać klasę do href lub podobnego, jeśli tekst linku kończy się na .mp3?jQuery dodaj klasę do href jeśli link zawiera konkretny tekst

Na przykład mam następujące linki w moim liście:

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

bym lubię wykrywać litery końcowe i dodawać różne klasy do linków, więc do linku, który ma tekst Document1.pdf Dodałbym klasę pdf do elementu kotwicy i link do tekstu Song1.mp3 Dodałbym klasę mp3 do elementu kotwicy.

Odpowiedz

35

Użyj selektora atrybutu:

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

opcje:

 
    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 o dodatkowe informacje.

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

Erm, chyba drugi powinien powiedzieć – Eoin

+0

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

+0

Edytowane, dziękuję. – Aioros

1

podane twoje wszystkie takie linki mają klasa .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]); 
}) 

Ten kod doda przedłużacza klasę do wszystkich takich linków, które mają rozszerzenie pliku w exts tablicy.

0

Zamiast ciężko kodowania wszystkich typów, można również rozwiązania, które automatycznie zrobić to dla wszystkich linków:

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