2016-05-17 24 views
8
'<button id="'+item['id']+'" class="btnDeactivateKeyInChildPremiumCustomer waves-effect waves-light>ok</button>' 

użyłem powyżej kodu do generowania przycisk wewnątrz jQuery każdy przycisk function.The tworzone dynamicznie i po kliknięciu przycisku, należy pokazać postęp na przycisk. Im przy użyciu tego Ladda Button Loader.Uncaught SyntaxError: Nie udało się wykonać „querySelector” na „dokument”

btnDeactivateKeyInChildPremiumCustomerClick : function(event){ 
     var id = event.currentTarget.id; 
     var btnProgress = Ladda.create(document.querySelector('#'+id)); 
//btnProgress.start(); or //btnProgress.stop(); 
    } 

a potem przeszły przycisk obsługi zdarzeń złapać przebiegu zdarzenia powyższy function.Inside tej funkcji spowoduje utworzenie btnProgress obiekt. Po tym można wywoływać funkcje start() lub stop(). Udało mi się pracować w przypadku tylko jednego przycisku bez dynamicznego tworzenia przycisku wewnątrz każdego z nich. Ale w każdym przypadku pokazuje błędy podczas wykonywania var btnProgress = Ladda.create (document.querySelector ('#' + id));

Błąd

Uncaught SyntaxError: Failed to execute 'querySelector' on 'Document': '#22' is not a valid selector. 

Odpowiedz

19

Wolno używać IDs that start with a digit w dokumentach HTML5:

The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

There are no other restrictions on what form an ID can take; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc.

Ale querySelector metoda wykorzystuje selektorów CSS3 dla zapytań DOM i CSS3 nie obsługuje identyfikator selektory rozpoczynające się cyfrą:

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.

Użyj wartości takiej jak b22 dla atrybutu ID, a Twój kod będzie działał.

Ponieważ chcesz, aby wybrać element, według ID można także użyć .getElementById metody:

document.getElementById('22') 
+0

więc w moim przypadku var btnProgress = Ladda.create (document.getElementById ('#' + id)); – boycod3

+0

@ boycod3 bez skrótu 'var btnProgress = Ladda.create (document.getElementById (id));' – jcubic

+1

Nie, nie powinieneś używać '#' dla metody 'getElementById'. Tylko identyfikator. – undefined