나는 쇼를 더 많이 사용하고 있습니다/코멘트를 적게 표시하고 있습니다. 목표는 분명히 사용자가 jquery에 대한 링크를 생성 할 때 발생하는 주석을 줄이고 길게 만드는 것입니다.show more/show less Jquery
문제 나는 여러 주석을 가지고 있고 jquery 코드 대신에 모든 주석에 대해 더 많은/더 적은 함수를 사용하여 데이터베이스에서 가져온 첫 번째 주석에 대해 데이터베이스에서 가져온 내용을 반복 한 다음 복제합니다 다른 이미지를 덮어 씁니다.
이 코드의 문제점은 무엇이며 수정 프로그램은 무엇입니까? HTML/PHP :
<p class="product-comment">$comments->comment</p>
jQuery를
$(document).ready(function(){
var showmoreHtml = $(".product-comment").html();
var showlessHtml = showmoreHtml.substr(0,400);
if(showmoreHtml.length > 400){
$(".product-comment").html(showlessHtml).append("<a href='' class='product-comment-more'> (...Show More)</a>");
}else{
$(".product-comment").html(showmoreHtml);
}
$("body").on("click", ".product-comment-more", function(event){
event.preventDefault();
$(this).parent(".product-comment").html(showmoreHtml).append("<a href='' class='product-comment-less'> (Show less)</a>")
});
$("body").on("click", ".product-comment-less", function(event){
event.preventDefault();
$(this).parent(".product-comment").html(showmoreHtml.substr(0,400)).append("<a href='' class='product-comment-more'> (...Show More)</a>")
});
});
각 기능을 사용해야합니다. 일부 문서 http://api.jquery.com/jquery.each/ –
여러 항목을 반환하는 선택기를 사용하고 있습니다. jQuery 함수는 함수에 따라 여러 항목이있을 때 다르게 작동합니다. 예를 들어'.html()'은 첫 번째 요소의 html을 가져오고, .html (text)는 모든 요소에 html을 설정합니다. 가장 쉬운 방법은 길이가 길고 길이가 짧은 두 개의 div가 있고 그것들을 토글하는 것입니다. –