2016-05-29 27 views
0

我有這個功能最初的作品,但當我改變頁面顯示隱藏元素時,該功能不能與那些曾經隱藏的元素一起工作。如何讓我的jQuery功能與隱藏元素一起使用時,它們變得可見

jQuery(document).ready(function($) { 

$(".paragraph-div").hide(); 

$(".toggle-div").click(function() 
        { 
if ($(this).next(".paragraph-div").is(":visible")) { 
    $(this).next(".paragraph-div").hide(); 
    $(this).text("More Info"); 
} else { 
    $(this).next(".paragraph-div").show(); 
    $(this).text("Less Info"); 
} 
}); 

}); 

我做了這裏的jsfiddle:https://jsfiddle.net/6k0bshb6/44/

因此,whats發生的是它僅適用於.paragraph-div的是最初看到在頁面加載,所以當我展示新段落的div(通過過濾表或使用我的桌子上顯示更多行按鈕),他們已經打開時,他們應該被隱藏和點擊功能不起作用。

我對jQuery相當陌生,不知道正確的方法來做到這一點,但我認爲它與("document").ready有關。我認爲我需要改變它,但是我該如何改變它?

如果你想看到你將需要用戶代理切換,並在http://mobilereactor.co.uk/shop/mobile-phones/apple-iphone-5s-32gb-space-grey-deals/

+1

刪除「'文件」':)周圍的引號 – 4castle

+0

我不明白這將如何工作在所有的第一位。傳遞給jQuery事件函數的第一個參數是事件對象,而不是jQuery對象。你正在使用第一個參數(事件對象),就好像它是整個函數中的jQuery一樣。 –

+2

@JohnDavidKievlan閱讀['.ready()'](https://api.jquery.com/ready/)的文檔並向下滾動到「混淆jQuery命名空間」。這是有效的。 – 4castle

回答

0

歡迎的移動用戶代理字符串作爲我的網站的移動版本工作IM我發現這個問題的答案jQuery - How do I bind events to hidden elements that will be shown later?問題

我不完全理解它 - 但它的工作原理!

我的解決方案;

//toggle details open and close 
$('body').on('click','.toggle-div',function() { 
    if ($(this).next(".paragraph-div").is(":visible")) { 
     $(this).next(".paragraph-div").hide(); 
     $(this).text("More Info"); 
    } else { 
     $(this).next(".paragraph-div").show(); 
     $(this).text("Less Info"); 
} 
}); 
相關問題