2016-08-25 82 views
0

我不明白爲什麼我的'閱讀更多'和'更少顯示'不會切換顯示。隱藏'閱讀更多'和'更少顯示'的標籤

我有更多標籤的多個實例,這裏是處理這個腳本:

<script> 
(function($) { 
$('.showcontent').click(function(e) { 
    $(this).parent().next('.cdscontainer').show(); 
    $(this).parent().next('.showcontent').hide(); 
     $(this).parent().next('.hidecontent').show(); 

}); 
$('.hidecontent').click(function(e) { 
    $(this).parent('.cdscontainer').hide(); 
    $(this).parent().next('.hidecontent').hide(); 
     $(this).parent().next('.showcontent').show(); 
}); 
})(jQuery); 
</script> 

該網站是www.kingkongco.com.au/c-cor/about-us(下工作人員圖片)

感謝您的任何幫助/建議!

+5

請提供相關的HTML,而不是你的網站的鏈接。 –

+1

再加上你的網站超慢。請提供一些代碼請 – kennasoft

+0

這些鏈接在您的網站上適合我。永遠加載,但它運行良好。您可以查看網站加載速度如此緩慢的原因。 – DelightedD0D

回答

1

可以更換next('p')next('.cdscontainer ')

$('.showcontent').click(function(){ 
 
    $(this).hide(); 
 
    $(this).parent().next('p').show(); 
 
}) 
 
$('.hidecontent').click(function(){ 
 
    $(this).parent().hide(); 
 
    $('.showcontent').show(); 
 
})
.cdscontainer { 
 
    display: none; 
 
    margin-top:-18px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p class="description">As General Manager,Volker is responsible for Engineering, Professional Services and day-to-day ef cient operations of the business including manufacturing, technical…<br> 
 
<a class="showcontent">Read more…</a></p> 
 
<p class="cdscontainer">…and engineering services, process management<br> 
 
and professional services delivery.<br> 
 
Recently, Volker was with Grass Valley USA, LLC where he held a technical leadership role as Regional Operations Manager AsiaPacific.<br> 
 
In 1993 he was a commercial and technical manager for the original Foxtel/Telstra CATV Rollout as part of Philips (Koninklijke Philips N.V.).<br> 
 
In this role he led vendor management for over 100 OEM vendors supplying Connectivity Equipment, Active Equipment and installation materials for the Telstra HFC Cable Network infrastructure rollout.<br> 
 
Significantly, he established service platforms for post-rollout support of the Telstra HFC Cable Network. Volker also project managed the Telstra Digital Video Network rollout program.<br> 
 
A highly capable executive with over 28-years professional experience in radio, telecommunications, broadband and television broadcast technologies.<br> 
 
Volker has extensive experience in system engineering, project management and delivery of professional services.<br> 
 
Volker was awarded a Graduate Diploma of Technology Management (Deakin University) and a Bachelor of Engineering – Electrical (Swinburne University of Technology).<br> 
 
<a class="hidecontent">…Hide Content</a></p>

+0

你先生,對我很好!非常感謝Charantej Golla! 當它明顯是一個JQ問題時,愛上其他人都在問html和css。 你有一個好心態。 (該網站很慢,因爲它與大約100個其他網站共享一個主機,只是爲了滿足任何人的好奇心) 我已經記下了您的更正,並期待在未來重複使用和分享您的建議, 再次感謝, – Khadesa

+0

歡迎Khadesa。我很高興我的答案用於你:) –

+0

我可能會如此貪婪,最後一次問你的魔法嗎? 我現在試圖通過添加樣式的短文本: ** $(「團隊P‘),CSS(’文本對齊」,‘左’); ** 但它影響所有團隊成員,我試過 ** $(this).parent()。next(「。team p」).css(「text-align」,「left」); ** 但是唉!無濟於事,有什麼想法? – Khadesa

0

要開始做事了,你的四行無可奈何:

$(this).parent().next('.showcontent').hide(); 
$(this).parent().next('.hidecontent').show(); 

$(this).parent().next('.hidecontent').hide(); 
$(this).parent().next('.showcontent').show(); 

看看jQuery的next function的描述。在第一種情況下,您將直接在.showcontent的父級(即.cdscontainer)之後選擇該元素。無論

$(this).parent().next('.showcontent') 

$(this).parent().next('.hidecontent') 

將是空集,因爲.showcontent的父之後的下一個元素既沒有這些類之一。因此,您的.show()和.hide()將不會執行任何操作,因爲沒有要執行的操作。

現在,爲了得到這個工作方式,他們想要它,你就需要在第一組的壞線改爲

$(this).hide(); //this is .showcontent 
$(this).parent().next().find('.hidecontent').show(); //Need to find the .hidecontent element since it is a child of parent's next element 

雖然您完全可以忽略第二行.hidecontent基於.cdscontainer的可見性隱藏/顯示。

第二組的壞線然後可以改爲

$(this).hide(); //this is .hidecontent, but, as above can be omitted since .cdscontainer is hidden 
$(this).parent().prev().find('.showcontent').show(); //Need to go to the parent's previous element and find child .showcontent since it appears in the DOM before .hidecontent 

現在,應該把你帶到你想要的,但要小心這種設置的。您的功能在HTML結構上非常重要。如果標記發生變化,父母/孩子不一樣,你可以看到這個突破。