最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show short text when text is long? - Stack Overflow

programmeradmin1浏览0评论

How to show certain length of text inside element and apend dots if text is longer for example than 200 letters. Then append ... as link after modified text. When user clicks on those dots show full text. Excuse me my english is very poor. HTML:

<div>
  <span="small2">TEXT MORE THEN 200 letters</span> <!-- Show 200 letters with dots as link -->
  <span="small2">TEXT LESS THEN 200 letters</span> <!-- Show full text without dots -->
</div>

My Jquery(not full because I am not pro):

$(".small2").each(function(){
    fulltext = $(this).text();
    if ($(this).text().length > 200) {
       $(this).text($(this).text().substr(0, 186));
       $(this).append('<a href="#">...</a>>');
    }
});

How to show certain length of text inside element and apend dots if text is longer for example than 200 letters. Then append ... as link after modified text. When user clicks on those dots show full text. Excuse me my english is very poor. HTML:

<div>
  <span="small2">TEXT MORE THEN 200 letters</span> <!-- Show 200 letters with dots as link -->
  <span="small2">TEXT LESS THEN 200 letters</span> <!-- Show full text without dots -->
</div>

My Jquery(not full because I am not pro):

$(".small2").each(function(){
    fulltext = $(this).text();
    if ($(this).text().length > 200) {
       $(this).text($(this).text().substr(0, 186));
       $(this).append('<a href="#">...</a>>');
    }
});
Share Improve this question edited Mar 25, 2014 at 11:46 Filburt 18.1k12 gold badges85 silver badges144 bronze badges asked Mar 25, 2014 at 11:43 user3342042user3342042 2411 gold badge7 silver badges20 bronze badges 5
  • 1 It's not too clear what you are asking here... Where are you having problems with this code? – Lix Commented Mar 25, 2014 at 11:46
  • I am not pro bro. It shows string with certain length with substr function. I am asking u if u can navigate me to success. – user3342042 Commented Mar 25, 2014 at 11:48
  • What's not working with the solution you have? – David Hedlund Commented Mar 25, 2014 at 11:50
  • 1 @user3342042 no one demands that you're a "pro", but please try to describe which parts of your code are causing problems. – lethal-guitar Commented Mar 25, 2014 at 11:51
  • Sorry I forgot to mention my code is not plete in functionality. It should show short text with dots if text is longer than 200. If user click on those dots it should show long text if user click once again It should collapse this text back to short text. – user3342042 Commented Mar 25, 2014 at 12:00
Add a ment  | 

3 Answers 3

Reset to default 6

A simple jQuery implementation:

$(".small2").each(function () {
    text = $(this).text();
    if (text.length > 200) {
        $(this).html(text.substr(0, 186) + '<span class="elipsis">' + text.substr(186) + '</span><a class="elipsis" href="#">...</a>');
    }
});
$(".small2 > a.elipsis").click(function (e) {
    e.preventDefault(); //prevent '#' from being added to the url
    $(this).prev('span.elipsis').fadeToggle(500);
});

HTML:

<div>
    <span class="small2">TEXT MORE THEN 200 letters TEXT MORE THEN 200 letters TEXT MORE THEN 200 letters TEXT MORE THEN 200 letters TEXT MORE THEN 200 letters TEXT MORE THEN 200 TEXT MORE THEN 200 TEXT MORE THEN 200 TEXT MORE THEN 200 TEXT MORE THEN 200</span> 
    <span class="small2">TEXT LESS THEN 200 letters</span> 
</div>

Fiddle: http://jsfiddle/KZ4TV/5/

Your html code is wrong, it should be like this:

<div>
  <span class="small2">qwertyuiopasdfghjklzxcvbnm</span> <!-- Show 200 letters with dots as link -->
  <span class="small2">abcd</span> <!-- Show full text without dots -->
</div>

It works: http://jsfiddle/MXC5L/

Use ID's for both spans
Here is the CSS for the span

#Span_ID

{
overflow: hidden;
white-space: nowrap;
-o-text-overflow: ellipsis;
-ms-text-overflow: ellipsis;
text-overflow: ellipsis;
width: 91px;
 }

here you can change width, which depends on the length of the text u want to show.

After this you have to include mootools-core-1.4.5-full-nopat.js file in your script tag. Because text-overflow:ellipsis won't work without this.

After this use jQuery ToolTip to show your plete text in the tooltip.

Here is the JSFIDDLE Example

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>