javascript/jQuery 找不到 ID 困惑的元素

标签 javascript jquery jquery-selectors dynamics-crm-2011

我正在尝试使用以下 ID 访问 MS CRM 2011 中的元素: account|NoRelationship|Form|B_GenerateInvoice-Large

我可以在 IE 开发者工具中看到这个元素: enter image description here

不幸的是,我在尝试查找此元素时总是得到 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 外..

我没有设法解决它。

最佳答案

jQuery Manual on Selectors状态:

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')...

jsFiddle Demo

编辑:我已经在 Chrome、Firefox 4、IE9、IE8 和 IE7 中成功测试了我的 Fiddle,它工作正常。

关于javascript/jQuery 找不到 ID 困惑的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6122014/

相关文章:

javascript - 计算数组 B 中主数组 A 中出现的次数

javascript - Typescript 更新对象内的项目

javascript - 无法通过 setTimeout 调用函数

javascript - 如何不在 <div> 上应用 CSS 文件? (不更改原始 CSS 文件。)

javascript - 在 React 中动态更改组件的子项

javascript - 除了悬停的 jQuery 之外的所有元素上切换类

javascript - Jquery中如何通过索引获取子元素?

jquery - 简单的 jQuery child 切换

jquery - 使用 JQuery 的 find 方法,如何根据 "foo"的子元素的内容选择某个 HTML 元素 "foo"的同级元素?

javascript - 错误 错误 : No value accessor for form control with unspecified name attribute