場景:?$("#" + AAA + ""),AAA代表某表單ID
當AAA為普通字符串時,ok;
當AAA含有特殊符號時(eg:a.b),獲取不到該對象;
原因:特殊符號會進行轉義,上面那種方式就無法獲取的該ID。
// Does not work:
$( "#some:id" )
// Works!
$( "#some\\:id" )
// Does not work:
$( "#some.id" )
// Works!
$( "#some\\.id" )
解決:
The following function takes care of escaping these characters and places a "#" at the beginning of the ID string:
function jq( myid ) {
return "#" + myid.replace( /(:|\.|\[|\]|,)/g, "\\$1" );
}
The function can be used like so:
$( jq( "some.id" ) )
http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/