這是您可以使用jQuery限製文本中的字符的方法。為任何頁面元素設置字符的最大長度是一個函數。例如,您可以使用它為博客上的帖子創建摘錄。請參閱更多jQuery。
demo
<span>(function($) { </span> <span>// jQuery function to set a maximum length or characters for a page element it can handle mutiple elements </span> $<span>.fn.createExcerpts = function(elems<span>,length,more_txt</span>) { </span> $<span>.each($(elems), function() { </span> <span>var item_html = $(this).html(); // </span> item_html <span>= item_html.replace(/< <span>/<span>?<span>[^>]</span>+></span>/gi</span>, ''); //replace html tags </span> item_html <span>= jQuery.trim(item_html); //trim whitespace </span> <span>$(this).html(item_html.substring(0,length)+more_txt); //update the html on page </span> <span>}); </span> <span>return this; //allow jQuery chaining </span> <span>} </span><span>})(jQuery);</span>
>
以上是使用jQuery為文本元素創建摘錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!