• March 20, 2018
    <div id="header">
    <H2>
    	Dynamically shortened Text with Show More link using jQuery
    </H2>
    </div>
    <br/>
    <div class="comment more">
    	Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    	Vestibulum laoreet, nunc eget laoreet sagittis,
    	quam ligula sodales orci, congue imperdiet eros tortor ac lectus.
    	Duis eget nisl orci. Aliquam mattis purus non mauris
    	blandit id luctus felis convallis.
    	Integer varius egestas vestibulum.
    	Nullam a dolor arcu, ac tempor elit. Donec.
    </div>
    <div class="comment more">
    	Duis nisl nibh, egestas at fermentum at, viverra et purus.
    	Maecenas lobortis odio id sapien facilisis elementum.
    	Curabitur et magna justo, et gravida augue.
    	Sed tristique pellentesque arcu quis tempor.
    </div>
    <div class="comment more">
    	consectetur adipiscing elit. Proin blandit nunc sed sem dictum id feugiat quam blandit.
    	Donec nec sem sed arcu interdum commodo ac ac diam. Donec consequat semper rutrum.
    	Vestibulum et mauris elit. Vestibulum mauris lacus, ultricies.
    </div>
    body, input{
    	font-family: Calibri, Arial;
    	margin: 0px;
    	padding: 0px;
    }
    a {
    	color: #0254EB
    }
    a:visited {
    	color: #0254EB
    }
    #header h2 {
    	color: white;
    	background-color: #00A1E6;
    	margin:0px;
    	padding: 5px;
    }
    .comment {
    	width: 400px;
    	background-color: #f0f0f0;
    	margin: 10px;
    }
    a.morelink {
    	text-decoration:none;
    	outline: none;
    }
    .morecontent span {
    	display: none;
    
    }
    $(document).ready(function() {
    	var showChar = 100;
    	var ellipsestext = "...";
    	var moretext = "more";
    	var lesstext = "less";
    	$('.more').each(function() {
    		var content = $(this).html();
    
    		if(content.length > showChar) {
    
    			var c = content.substr(0, showChar);
    			var h = content.substr(showChar-1, content.length - showChar);
    
    			var html = c + '<span class="moreelipses">'+ellipsestext+'</span>&nbsp;<span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a href="" class="morelink">'+moretext+'</a></span>';
    
    			$(this).html(html);
    		}
    
    	});
    
    	$(".morelink").click(function(){
    		if($(this).hasClass("less")) {
    			$(this).removeClass("less");
    			$(this).html(moretext);
    		} else {
    			$(this).addClass("less");
    			$(this).html(lesstext);
    		}
    		$(this).parent().prev().toggle();
    		$(this).prev().toggle();
    		return false;
    	});
    });

    http://jsfiddle.net/X5r8r/1156/



เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories