W skrócie
$(document.getElementById("test:abc")) jest tym, czego powinieneś użyć.
Wyjaśnienie : Oprócz przyrostu prędkości (patrz poniżej) jest łatwiejszy w obsłudze.
Przykład: załóżmy, że masz funkcję
function doStuff(id){
var jEle = $("#" + id); //is not safe, since id might be "foo:bar:baz" and thus fail.
//You would first have to look for ":" in the id string, then replace it
var jEle = $(document.getElementById(id)); //forget about the fact
//that the id string might contain ':', this always works
}
//just to give an idea that the ID might be coming from somewhere unkown
var retrievedId = $("foo").attr("data-target-id");
doStuff(retrievedId);
Prędkość / czas
spójrz na ten plik jsbin, który testuje i porównuje szybkość metod wyboru identyfikatorów z dwukropkami
aby uzyskać wyniki, musisz otworzyć konsolę firebuga.
Przetestowałem to z Firefox 10 i jQuery 1.7.2
w zasadzie zrobiłem 10 000 razy div z dwukropkiem w id - z różnymi metodami, aby to osiągnąć. Następnie porównałem wyniki do selekcji ID bez dwukropka, wyniki są dość zaskakujące.
lewy czas w ms prawa metoda selektora
299 $("#annoying\\:colon")
302 $("[id='annoying:colon']"
20 $(document.getElementById("annoying:colon"))
71 $("#nocolon")
294 $("[id='nocolon']")
szczególnie
71 $("#nocolon") and
299 $("#annoying\\:colon")
jest trochę zaskoczeniem