2013-10-09 34 views
0

這是非常愚蠢的問題。但我是新來的HTML,jQuery。任何人都可以給我這個解決方案。如何在點擊時關閉當前元素

如何關閉上點擊

<a>test</a> 

的元素,當我點擊測試表明關閉/隱藏()

我嘗試作爲this.hide() onClick但它沒有工作了。 (<a onclick='this.hide()'>test</a>)

+2

[如何jQuery的作品](http://learn.jquery.com/about-jquery/how-jquery-works/)。 – undefined

回答

3

.hide()是jQuery提供的方法,它在dom元素中不可用。

<a onclick='$(this).hide()'>test</a> 
7

單擊處理程序this將不具備hide()方法本土DOM元素 - 你需要把它變成一個jQuery對象。試試這個:

<a href="#" class="foo">test</a> 
$('.foo').click(function(e) { 
    e.preventDefault(); 
    $(this).hide(); 
}); 

夫婦的說明;首先使用onclick屬性已過時。如果你使用jQuery,用它來連接你的事件。

其次,a元素必須有hrefname屬性,因此你需要使用event.preventDefault()當它點擊停止默認行爲。

0

嘗試這樣使用jQuery:

<a class"some_link">test</a> 

$(function(){ 
    $(".some_link").click(function(){ 
     $(this).hide(); // $(this).remove(); to remove 
    }); 
}); 
相關問題