简体   繁体   English

如何使用jQuery选择器获取此元素?

[英]How to get this element using jQuery selectors?

I use jQuery to get values of presaved elements from some websites, using paths like this: 我使用jQuery通过类似这样的路径从某些网站获取预先保存的元素的值:

HTML BODY #bodyContainer #mainContentContainer #mainContent #productContentRight #swatchContent #colorSwatchContent SPAN HTML正文#bodyContainer #mainContentContainer #mainContent #productContentRight #swatchContent #colorSwatchContent SPAN

The problem i faced when the websites page contains tables and there are same element in another similar path such as: 当网站页面包含表格并且在另一个类似的路径中有相同的元素时,我遇到的问题是:

/html/body/div/center/div/div[3]/div/table/tbody/tr[5]/td/div/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[3]/td / html / body / div / center / div / div [3] / div / table / tbody / tr [5] / td / div / table / tbody / tr / td / table / tbody / tr / td / table / tbody / tr [3] / td

In the last path as you can see that there are 5 tr which means that its possible to find the same element in another path. 如您所见,在最后一条路径中有5 tr ,这意味着可以在另一条路径中找到相同的元素。

I use the path as a selector for jQuery and jQuery will return array of elements, i don't know which one is the right element. 我使用路径作为jQuery的选择器,并且jQuery将返回元素数组,但我不知道哪一个是正确的元素。

So my question is: 所以我的问题是:

How to save the path for better later use? 如何保存路径以便以后使用? and how to parse this new path to be ready as a jQuery selector. 以及如何解析此新路径以用作jQuery选择器。

If the question is not clear please ask me and i will do my best to explain more. 如果问题不清楚,请问我,我将尽力解释更多。

I don't know why there are so many answers that you are using XPath because XPath was deprecated a long time ago and jQuery no longer supports it without the XPath compatibility plugin. 我不知道您为什么使用XPath这么多答案,因为XPath很久以前已被弃用,而jQuery在没有XPath兼容性插件的情况下不再支持它。

See Release Notes of 1.2 : http://www.learningjquery.com/2007/09/upgrading-to-jquery-12 请参阅1.2的发行说明: http : //www.learningjquery.com/2007/09/upgrading-to-jquery-12

XPath compatibility plugin : http://docs.jquery.com/Release:jQuery_1.2#XPath_Compatibility_Plugin XPath兼容性插件: http : //docs.jquery.com/Release : jQuery_1.2#XPath_Compatibility_Plugin

Just use $("#colorSwatchContent span") as your selector. 只需使用$("#colorSwatchContent span")作为选择器即可。 Which is a css style seclector meaning find me all descendent span elements of an element with id colorSwatchContent. 这是一个css样式分隔符,含义是为我找到ID为colorSwatchContent的元素的所有后代span元素。 Since id's in html are unique identitfiers, this is about as specific as you can get. 由于html中的id是唯一的标识符,因此您将获得尽可能具体的ID。

$("#colorSwatchContent > span") will only select DIRECT descendents (immedieate children) $("#colorSwatchContent > span")将仅选择DIRECT后代(有近代的孩子)

$("#colorSwatchContent > span:first") will select the first span direct descendent $("#colorSwatchContent > span:first")将选择第一个span直接后代

In order to grab one specific element when there are many that match you should give the elements classes, for example give each table a class describing what is in it, then give each tr a class describing what the row is about. 为了在有许多匹配的元素时抓住一个特定的元素,您应该给元素类一个类,例如给每个table一个类来描述其中的内容,然后给每个tr一个类来描述行的含义。 Then each td with a class describing the specific part of the row that it describes, for example: 然后,每个td都有一个类来描述它描述的行的特定部分,例如:

<table class="person">
    <tr class="john-doe">
        <td class="name">John Doe</td>
        <td class="phone-numbers">
            <table class="phone-numbers">
                <tr class="cell-phone">
                    <th class="label">Cell Phone:</th>
                    <td class="number">555-1234</td>
                </tr>
                <tr class="home-phone">
                    <th class="label">Home Phone:</th>
                    <td class="number">555-1234</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

Once you have your elements properly described then you can use CSS style selectors in jQuery. 一旦正确描述了元素,就可以在jQuery中使用CSS样式选择器。 for example getting just the td that has the home phone would be as simple as doing: 例如,仅获取具有家用电话的td就像这样做一样简单:

$('table.person tr.home-phone td.number');

Hope this gets you heading the right way. 希望这能使您正确地前进。

One thing to note tho, If you have incredibly complex table structures you might want to rethink whether it needs to be in a table or not. 需要注意的一件事是,如果您具有非常复杂的表结构,则可能需要重新考虑它是否需要在表中。

tr[5] doesn't mean there are 5 trs (there could be 10!), it means that it is selecting the 5th one. tr [5]并不意味着有5个trs(可能有10个!),这意味着它正在选择第5个。

It looks to me like you are using an XPath selector to get your elements... which jQuery supports. 在我看来,您正在使用XPath选择器来获取元素... jQuery支持。

if you have control of the HTML, the easiest way to select a specific element is to give it an id... which in your first example, 如果您可以控制HTML,则选择特定元素的最简单方法是为其指定一个ID ...在第一个示例中,

HTML BODY #bodyContainer #mainContentContainer #mainContent #productContentRight #swatchContent #colorSwatchContent SPAN

is equivilant to 等同于

#colorSwatchContent SPAN

Since jQuery supports xpath you could use firebug to get the specific xpath, and then use that in jQuery. 由于jQuery支持xpath,因此您可以使用firebug获取特定的xpath,然后在jQuery中使用它。

Just browse the source in firebug, right click any element, and then choose copy xpath. 只需在firebug中浏览源代码,右键单击任何元素,然后选择copy xpath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM