使用Javascript / jQuery访问具有不正确的id属性的HTML元素 [英] Using Javascript/jQuery to access HTML elements with improper id attribute
问题描述
我正在制作一个Greasemonkey脚本,让某人更改CRM(Zoho)创建的一些字段的显示,因为他们无权访问更改呈现的HTML。
这应该很容易,但Zoho认为创建合适的HTML对屁股来说太痛苦了,我猜,他们的HTML包含这样的东西:
< input type =textmaxlength =50style =width:100%; class =textFieldid =property(Phone)name =property(Phone)/>
这些ID包含空格和括号,这些空格和括号在ID属性中无效,并阻止我从使用document.getElementById()来选择它们,或者使用jQuery来选择它们。
有没有人对我如何获取该元素有任何想法?显然,我可以通过它的父元素中的索引或者遍历DOM来抓取它,但这两个都意味着如果字段的顺序改变了,那么Greasemonkey脚本就会停止正常工作,因为它会针对错误的元素。
您可以使用反斜杠来转义空格和括号:
<$ ($'code> $('#property\\(Phone \\')')。val('jQuery selected property(Phone)!');
$('#ab \\ cd\\ ef')。val('jQuery selected ab cd ef!');
I'm making a Greasemonkey script for someone to change the display of some of the fields their CRM(Zoho) created because they don't have access to change the rendered HTML.
This should be easy, BUT Zoho decided that creating proper HTML was too big a pain in the ass, I guess, and their HTML contains things like this:
<input type="text" maxlength="50" style="width: 100%;" class="textField" id="property(Phone)" name="property(Phone)"/>
The ID's contain spaces and parentheses, which aren't valid in ID attributes, and is preventing me from using either document.getElementById() to select them or from using jQuery to select them.
Does anyone have any ideas for how I could grab that element? Obviously I could grab it via its index in its parent element, or by traversing the DOM, but both of those would mean that if the order of the fields changed, the Greasemonkey script would stop working correctly because it would then be targeting the wrong elements.
You can escape the spaces and parentheses using backslashes:
$('#property\\(Phone\\)').val('jQuery selected property(Phone)!');
$('#ab\\ cd\\ ef').val('jQuery selected ab cd ef!');
这篇关于使用Javascript / jQuery访问具有不正确的id属性的HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!