2013-03-06 86 views
0

有一個jquery對象 - 錶行,我怎麼才能知道錶行(tr)中的第一個元素是否包含鏈接?jquery檢查錶行中第一個td元素是否包含鏈接

樣品HTML:

<tr class=""><td class=""><p><a>some link</a></p></td><td class="pos">AG</td><td class="size">5.1 MB</td><td class="options"><a class="tip" href="/Packages/View/2">View</a></td></tr> 

VS:具有jquery對象

<tr class=""><td class=""><p>no link here</p></td><td class="pos">AG</td><td class="size">5.1 MB</td><td class="options"><a class="tip" href="/Packages/View/2">View</a></td></tr> 

「行」 我想這沒有任何的運氣:

var firstTdLink = row.find("td:first a") 
if (firstTdLink.length) 
{ 
alert("first td contains link") 
} 
+0

我是gues因爲你的代碼工作正常,所以你在分配'row'時會出現問題 - 請參閱下面的答案。 – 2013-03-06 07:26:10

回答

3

隨着以下:

row.find('td:first-child:has(a)'); 
+0

他們都工作,但你是第一個。謝謝 – ShaneKm 2013-03-06 07:45:46

1

你原來的代碼工作正常,我:

<table> 
<tr class=""><td class=""><p><a>some link</a></p></td><td class="pos">AG</td><td class="size">5.1 MB</td><td class="options"><a class="tip" href="/Packages/View/2">View</a></td></tr> 
<tr class=""><td class=""><p>no link here</p></td><td class="pos">AG</td><td class="size">5.1 MB</td><td class="options"><a class="tip" href="/Packages/View/2">View</a></td></tr> 
</table> 

<script> 
console.log($('table tr:first-child').find("td:first a").length) 
</script> 
2

嘗試了這一點

if($("tr td:first > a").length>0) 
{ 
    alert("first td contains link") 
} 
0

使用直系後裔選擇:

if($('tr td:first > a').length > 0) 
{ 
    alert("first td contains link") 
} 
+0

不應該在這種情況下有所作爲,而不僅僅是'$('tr td:first a')' – 2013-03-06 07:33:08

0

我想是這樣的:

var firstTdLink = $("td:first").find('a'); 
if (firstTdLink.length > 0){ 
    alert("first td contains link") 
} 
+0

這相當於OP的代碼,除非它不限制它到一個特定的行......也是這樣可以更簡潔:'$(「td:first a」)'但是無論如何,我認爲從一個特定的'row'變量開始的想法很重要...... – 2013-03-06 07:31:53

相關問題