Menu
Hello,
I have this complex CSS selector that I use to identify a specific tag:
[CODE]body>table:nth-of-type(3)>tbody>tr:last-child>td>table:first-of-type>tbody>tr:last-child>td:first-child
How could I select the same tag using Javascript? ?
If you have links to material that could help me understand how that works in JS, I would appreciate also.
Thanks!
<i>
</i>body>table:nth-of-type(3)>tbody>tr:last-child>td>table:first-of-type>tbody>tr:last-child>td:first-child
<i>
</i>var myrows=document.getElementsByTagName('table')[3].getElementsByTagName('tr');
var mycell=myrows[myrows.length-1].getElementsByTagName('td')[0];
var xpathExpr = '//body/table[position()=3]/tbody/tr[position()=last()]/td/table[position()=1]/tbody/tr[position()=last()]/td[position()=1]';
var cellIterator = document.evaluate(xpathExpr, document, null, XPathResult.ANY_TYPE, null);
var myCells = new Array(), currentCell = cellIterator.iterateNext();
while (currentCell) {
myCells.push(currentCell);
currentCell = cellIterator.iterateNext();
}
var xpathExpr = '//div[@align="left"]';
var xpathIter = document.evaluate(xpathExpr, document, null, XPathResult.ANY_TYPE, null);
var myDiv, currentDiv = xpathIter.iterateNext();
while (currentDiv) {
if (currentDiv.clientWidth == 10) {
myDiv = currentDiv;
break;
}
currentDiv = xpathIter.iterateNext();
}
// Do something with 'myDiv'.
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
<i> </i>$("body>table:eq(2)>tbody>tr:last-child>td>table:first-of-type>tbody>tr:last-child>td:first-child").eq(0).[i]someJQueryMethod()[/i];
<i> </i>// or
<i> </i>var domNode = $("body>table:eq(2)>tbody>tr:last-child>td>table:first-of-type>tbody>tr:last-child>td:first-child")[0];
});
</script>
$("div[align='left']").filter(function(){ return $(this).width() == 10 });
jQuery will do the job alright, but don't be fooled, it'll take just as long in the browser (if not longer), as the code that Kor has shown you.[/quote]
My target with the XPath code was pretty much anything ... except IE[/quote]
Unfortunately that means your code won't work for 65-85% of users (depending on where you get your stats). I don't think any client would ever be okay with that, nor should any developer.[/quote]
0.1.9 — BETA 5.20