2009-12-11 52 views
0

我正在嘗試獲取下一個元素的innerHTML,在其旁邊發生了Click。使用Prototype獲取點擊事件上next()元素的innerHTML

到目前爲止,我有這個作爲我的代碼

$('translate_this').observe('click', function(e) { 
    // Immediately stop the click through 
    Event.stop(e); 
    x = this.next().innerHTML; 
    alert(x);  
}); 

但是,沒有實際發生,這表明該變量沒有被任何東西填充的。爲什麼是這樣。

HTML參考

<li class="say public"> 
<p> 
    <span style="padding: 2px 7px; background-color: rgb(230, 230, 230); -moz-border-radius-topleft: 6px; -moz-border-radius-topright: 6px; -moz-border-radius-bottomright: 6px; -moz-border-radius-bottomleft: 6px;"> 
     <a id="translate_this" href="">Translate</a> 
    </span> 
</p> 
<div class="post_non_translated"> 
    <p>Bonjour </p> 
</div> 
</li> 

回答

2

你的問題是,有你的情況下,沒有下一個元素。你必須穿越到<li>然後下到<div>獲得「卓悅」

試試這個:

var x = this.up('li').down('.post_non_translated').innerHTML; 

,這將給你<p>Bonjour</p>,然後你可以細化它,如果你越是想擺脫的p

+0

嘎!當然,現在這很合理 - 謝謝! – jakeisonline 2009-12-11 12:41:44

0

問題是,在你的例子中,translate_this元素沒有任何兄弟姐妹。它是span元素的唯一子元素。

0

next()以節點樹的方式找到下一個節點;不只是從上到下。 translate_this是跨度的唯一孩子,因此它沒有兄弟姐妹,並且next將返回空值。您必須從parent回到pnext