未捕获的SyntaxError:未能执行对“文档”
'<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.
你被允许在你的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')
所以在我的情况 变种btnProgress = Ladda.create(的document.getElementById( '#' + ID)); – boycod3
@ boycod3 without the hash'var btnProgress = Ladda.create(document.getElementById(id));' – jcubic
不,你不应该使用''''getElementById'方法。只是ID。 – undefined