开发者

Why does jQuery 1.3.2 not find an existing control by ID - what am I doing wrong?

开发者 https://www.devze.com 2022-12-11 17:26 出处:网络
I have a complex html-Form and am using jQ for some of the work there. And spent the best part of this afternoon trying to understand a problem which I was able to trace back to the point where jQuery

I have a complex html-Form and am using jQ for some of the work there. And spent the best part of this afternoon trying to understand a problem which I was able to trace back to the point where jQuery failed to find an existing control. The command $("#FormView1_CopaBOM973row%18%_dkF").width(); returned null. First I thought about an error in the width()-implementation and tried $("#FormView1_CopaBOM973row%18%_dkF").attr("id"); - which did not return anything and its length was 0.

Im doing this in Firefox 3.5.5, have used Firebug's DOM-Explorer to verify the thing was there. And finally, the ultimate test worked: $(document.getElementById("FormView1_CopaBOM973row%18%_dkF")).attr("id") returned the expected result.

So, my only idea would be that any "suspicious" character开发者_开发问答s would need escaping, but the doc doesn't mention underscore or percent as "suspicous". So, where's my fault?

And pls. don't ask for more "code", I don't think this issue would need any more...


As Matthew said, special characters are not allowed on the ID attribute. This is not a jQuery problem. Here is the complete reference.


It appears that the list of special characters at the bottom of this page is incomplete. Try escaping your percent signs:

$("#FormView1_CopaBOM973row\\%18\\%_dkF")


try $("#FormView1_CopaBOM973row%18%_dkF")[0].id;


'%' is not a valid character for an id attribute so probably jQuery doesn't like that.


Escaping the percent signs seems to work.

$("#FormView1_CopaBOM973row\\%18\\%_dkF")


I think its because of special characters you have in your ID.

Try:

$("[id=FormView1_CopaBOM973row%18%_dkF]").width();


Those percents % are apparently illegal in CSS selectors (apparently because I can't find them in w3 spec, or it must be an unrevealed jQuery bug), but not in HTML attribues. Escaping them does the work though:

$("#FormView1_CopaBOM973row\\%18\\%_dkF").width();
0

精彩评论

暂无评论...
验证码 换一张
取 消