> On the other hand, can you find any better way to mimic `p.foo` than
> the following?:
I would just use //p[@class = "foo"]
Of course, this looks for a class attribute whose value is only "foo".
Outside of jQuery I see absolutely no value in supplying multiple values
to a class attribute. But if you did you could use this XPath:
p[contains(@class, 'foo')]
It's not quite as simple as p.foo, but it's not challenging either. It
does beg the question: jQuery aside why would I ever use that except if
I did not know the full class value?
> The only one of your list that I miss is parent. And that only
> rarely.
I need access to ancestors all the time. Consider this example:
<div class="box">
<h3>Some title</h3>
<p class="buttons">
<button onclick="myfunction(this)">Trigger</button>
</p>
</div>
The button click triggers an action. How do I find that h3 element from
The button and not other h3 elements using a CSS selector? Its common
for an element generating an event to look to the status of some nearby
ancestor element, such as if the class on the div element has changed
value. This can be summed up by saying I don't know what to do in the
absence of my family.
Thanks,
Austin Cheney, CISSP
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of Scott Sauyet
Sent: Thursday, September 08, 2011 2:03 PM
To: The JSMentors JavaScript Discussion Group
Subject: [JSMentors] Re: spaces in attribute values
Austin Cheney wrote:
> Scott Sauyet wrote:
>> how often do we really need to target something that can't be
>> expressed with CSS?
>
> All the time. How can I target the following with CSS?
>
> * parent
Can't do this, by design. This would be the primary advantage to
XPath
> * last child
p span:last-child
> * prior sibling
Can't do this, by design.
> * next sibling
p + *
> * first n named child of parent j
p > span:first-child
> * third n named child of parent j
p > span:nth-child(42)
> * first descendant of any name with attribute n and attribute value j
I don't know how to do this exactly, but this will get all such
descendents, in document order, so you can easily then select the
first one: p *[n='j']
> * first element where name contains "abc"
Can't be done, probably not by design, but because it's a very strange
request. But you can do this with attribute values.>
> Name, in the above examples, refers to element name and not HTML name
> Attribute.
> As far as I know you can't do any of these with CSS, and the first is
> absolutely essential. I am pretty sure all these are immediately
> achievable in XPath. I need most of those pretty frequently and have to
> write conditional code in JavaScript loops to achieve some of these that
> XPath performs in a single simple expression that is faster to execute.
The only one of your list that I miss is parent. And that only
rarely.
> I bet you need most of those too and probably just don't consider it as
> a selector when you are forced to write some amount of nested logic to
> accomplish similar objectives where such logic looks nothing like a
> selector.
On the other hand, can you find any better way to mimic `p.foo` than
the following?:
//p[contains(concat(' ',normalize-space(@class),' '),' foo ')]
:-)
-- Scott
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]