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

に私はjqueryのの内側に、動的に作成された各function.Theボタンをボタンを生成するためのコードの上に使用し、私はボタンをクリックしたときに、それは上の進行状況を表示する必要がありますボタン。 これを使用しているIm Ladda Button LoaderキャッチされないでSyntaxError:実行に失敗しました「querySelector」「ドキュメント」

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

し、私は、イベントハンドラは、イベント処理にそれがbtnProgressオブジェクトを作成する関数上記function.Insideをキャッチボタンを可決しました。 その後、start()またはstop()functions.Iを呼び出すことができます。ボタンを動的に作成せずにボタンを1つだけ操作した場合、正常に機能しました。しかし、それぞれのケースでは、実行中にいくつかのエラーが表示されています。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方法は、DOMやCSS3を照会するための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のような値を使用すると、コードが機能します。 VAR btnProgress = Ladda.create(のdocument.getElementById( '#' + ID))私の場合はそう

document.getElementById('22') 
+0

;:あなたはIDによって要素を選択したいので

あなたも.getElementByIdメソッドを使用することができます – boycod3

+0

@ boycod3ハッシュなし 'var btnProgress = Ladda.create(document.getElementById(id));' – jcubic

+1

いいえ、 'getElementById'メソッドには'# 'を使わないでください。ちょうどID。 – undefined

関連する問題