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

我上面的代碼用於生成的jquery的內部按鈕每個function.The按鈕動態創建的,並且當我點擊該按鈕時,它應該能顯示在進度「querySelector」按鈕。 Im使用Ladda Button Loader未捕獲的SyntaxError:未能執行對「文檔」

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

然後,我通過該按鈕的事件處理程序捕捉事件的過程中,上述function.Inside發揮功能,將創建一個btnProgress對象。 之後,我可以調用start()或stop()函數。我已經成功地在只有一個按鈕的情況下工作,而不需要在每個按鈕內動態地創建按鈕。但在每種情況下,它執行時顯示一些錯誤var btnProgress = Ladda.create(document.querySelector('#'+ id));

錯誤

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

回答

19

你被允許在你的HTML5文檔中使用IDs that start with a digit

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.

querySelector方法使用CSS3選擇器來查詢DOM和CSS3不支持ID以數字開頭的選擇器:

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.

對ID屬性使用類似b22的值,並且您的代碼將起作用。

由於要選擇由ID的元件也可以用.getElementById方法:

document.getElementById('22') 
+0

所以在我的情況 變種btnProgress = Ladda.create(的document.getElementById( '#' + ID)); – boycod3

+0

@ boycod3 without the hash'var btnProgress = Ladda.create(document.getElementById(id));' – jcubic

+1

不,你不應該使用''''getElementById'方法。只是ID。 – undefined