我正在尝试使用以下 ID 访问 MS CRM 2011 中的元素: account|NoRelationship|Form|B_GenerateInvoice-Large
我可以在 IE 开发者工具中看到这个元素:
不幸的是,我在尝试查找此元素时总是得到 null。
我试过以下方法:
alert(document.getElementById('account|NoRelationship|Form|B_GenerateInvoice-Large'));
alert($("[id='account|NoRelationship|Form|B_GenerateInvoice-Large]").html());
alert($(jq("account|NoRelationship|Form|B_GenerateInvoice-Large")).html()); // jq() adds the '#' and escapes special characters
alert($("#account|NoRelationship|Form|B_GenerateInvoice-Large").html());
alert(document.getElementById("#account\\|NoRelationship\\|Form\\|B_GenerateInvoice-Large"));
alert($("#account\\|NoRelationship\\|Form\\|B_GenerateInvoice-Large").html());
这些都找不到元素。
我是否遗漏了一些明显的东西?
解决方案:
javascript 在 iframe 内,而元素在 iframe 外..
我没有设法解决它。
最佳答案
If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an element with id="foo.bar", you can use the selector $("#foo\\.bar"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors.
那么试试这个:
$('#account\\|NoRelationship\\|Form\\|B_GenerateInvoice-Large')...
编辑:我已经在 Chrome、Firefox 4、IE9、IE8 和 IE7 中成功测试了我的 Fiddle,它工作正常。
关于javascript/jQuery 找不到 ID 困惑的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6122014/