首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否使用javascript隐藏/显示文本?

是否使用javascript隐藏/显示文本?
EN

Stack Overflow用户
提问于 2019-07-30 02:29:57
回答 3查看 72关注 0票数 1

我在尝试切换文本时遇到了一个问题。我有一个被截断的文本,如何在单击按钮时在截断文本和原始文本之间来回切换?

link到画笔。

代码语言:javascript
复制
var myButton = document.getElementById('toggle_text')
var text = document.getElementById('original_text')
console.log(text)

var truncate = function(elem, limit, after) {

  // Make sure an element and number of items to truncate is provided
  if (!elem || !limit) return;

  // Get the inner content of the element
  var content = elem.textContent.trim();

  // Convert the content into an array of words
  // Remove any words above the limit
  content = content.split(' ').slice(0, limit);

  // Convert the array of words back into a string
  // If there's content to add after it, add it
  content = content.join(' ') + (after ? after : '');

  // Inject the content back into the DOM
  elem.textContent = content;

};

var elem = document.querySelector('.truncate');
truncate(elem, 7, '...');


function switchText() {

}
代码语言:javascript
复制
<div class="truncate" id="original_text">
  Port tender gun spanker lanyard heave to topmast. Heave down draught piracy loaded to the gunwalls mizzenmast topsail Brethren of the Coast. Lanyard snow Jack Ketch swing the lead maroon spike black jack.
</div>

<div>
  <button id="toggle_text" onClick='switchText()'>Toggle Between truncate and Original Text</button>
</div>

先谢谢你们了。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-30 02:35:42

您可以将完整内容文本和截断状态保存在变量中,如下所示:

代码语言:javascript
复制
var myButton = document.getElementById('toggle_text')
var text = document.getElementById('original_text')
console.log(text)

var truncate = function(elem, limit, after) {

  // Make sure an element and number of items to truncate is provided
  if (!elem || !limit) return;

  // Get the inner content of the element
  var content = elem.textContent.trim();

  // Convert the content into an array of words
  // Remove any words above the limit
  content = content.split(' ').slice(0, limit);

  // Convert the array of words back into a string
  // If there's content to add after it, add it
  content = content.join(' ') + (after ? after : '');

  // Inject the content back into the DOM
  elem.textContent = content;
  truncated = true;
};

var elem = document.querySelector('.truncate');
var fullText = elem.textContent;
var truncated = false;
truncate(elem, 7, '...');


function switchText() {
    var elem = document.querySelector('.truncate');
    if (truncated) {
      elem.textContent = fullText;
      truncated = false;
    } else {
      truncate(elem, 7, '...');
    }
     
}
代码语言:javascript
复制
<div class="truncate" id="original_text">
  Port tender gun spanker lanyard heave to topmast. Heave down draught piracy loaded to the gunwalls mizzenmast topsail Brethren of the Coast. Lanyard snow Jack Ketch swing the lead maroon spike black jack.
</div>

<div>
  <button id="toggle_text" onClick='switchText()'>Toggle Between truncate and Original Text</button>
</div>

票数 2
EN

Stack Overflow用户

发布于 2019-07-30 02:41:56

你可以用更少的代码来实现这一点:

代码语言:javascript
复制
var myButton = document.getElementById('toggle_text');
var textCont = document.getElementById('original_text');
var text = textCont.innerText;
var shortText = text.substring(0, 7) + '...';

myButton.addEventListener('click', function() {
  if (textCont.innerText === text) {
    textCont.innerText = shortText;
  } else {
    textCont.innerText = text;
  }
}, false);
代码语言:javascript
复制
<div class="truncate" id="original_text">
  Port tender gun spanker lanyard heave to topmast. Heave down draught piracy loaded to the gunwalls mizzenmast topsail Brethren of the Coast. Lanyard snow Jack Ketch swing the lead maroon spike black jack.
</div>

<div>
  <button id="toggle_text">Toggle Between truncate and Original Text</button>
</div>

票数 2
EN

Stack Overflow用户

发布于 2019-07-30 02:51:17

我会考虑在similar question上查看S.Serpooshan's answer。通常,只使用CSS就可以实现这一点,这将是维护页面状态的更好方法。

无需将文本作为变量存储在JS中,只需将其隐藏在DOM流中,但仍可轻松访问。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57259572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档