2016-11-21 148 views
0

我正在處理更多/更少的jQuery文本按鈕。當我的jQuery腳本加載時,我只想顯示div中的部分文本。在這種情況下,最初要隱藏的任何文本> 650個字符。我無法弄清楚如何使用jQuery隱藏超出該限制的文本。另外,我正在嘗試創建一個函數,在單擊按鈕時顯示div中的其他文本。然後,當單擊相同的按鈕時,文本將只顯示最初顯示的內容。jQuery隱藏/顯示更多按鈕

我有一個小提琴,我一直在努力,可能是在正確的軌道上,但我不確定是否是這種情況。

如果您有任何建議,請讓我知道。

小提琴------ https://jsfiddle.net/carbot3000/f20bbh7a/99/

$(document).ready(function() { 
var showChar = 250; // How many characters are shown by default 
var ellipsestext = "..."; 
var rvwbody = jQuery(".reviewbody").html(); 
var c = rvwbody.substr(0, showChar); 
var h = rvwbody.substr(0, rvwbody.length); 

jQuery('.reviewbody').each(function() { 
    if (rvwbody.length > showChar) { 
    jQuery(".btnRead").show(); 
    jQuery(".reviewbody").html(h); 
} 
}); 
jQuery(".btnRead").click(function() { 
jQuery(".btnRead").html(jQuery('.btnRead').text() == 'Hide me' ? 'Show Me' : 'Hide me'); 
jQuery(".reviewbody").html(h); 
jQuery(".tstars").toggleClass("color"); 
console.log(h); 
}); 
}); 
+0

https://codepen.io/maxds/pen/jgeoA請使用 – Daidon

回答

3

試試這個

$(document).ready(function() { 
    var showChar = 250; // How many characters are shown by default 
    var ellipsestext = "..."; 
    var rvwbody = jQuery(".reviewbody").html(); 
    var c = rvwbody.substr(0, showChar); 
    var h = rvwbody; 

    jQuery('.reviewbody').each(function() { 
    if (rvwbody.length > showChar) { 
     jQuery(".btnRead").show(); 
     jQuery(".reviewbody").html(c); 
    } 
    }); 
    jQuery(".btnRead").click(function() { 
    jQuery(".btnRead").html(jQuery('.btnRead').text() == 'Hide me' ? 'Show Me' : 'Hide me'); 
    jQuery(".reviewbody").html(h); 
    jQuery(".tstars").toggleClass("color"); 
    console.log(h); 
    }); 
+0

...我很抱歉甚至問這個問題。我忘了將變量從h更改爲c。感謝您的幫助,我不能相信我錯過了! –