【问题标题】:如何在 JavaScript 中编写 Show more and show less
【发布时间】:2022-01-23 14:25:28
【问题描述】:

我有这样的cshtml代码

  <span class="p1">
                    Breaking India: Western Interventions in Dravidian and Dalit Faultlines is a book written by Rajiv Malhotra and Aravindan Neelakandan which argues that India's integrity is being undermined
                    <button class="More">..Show More</button>
                </span>

按下跨度内的按钮时,它将显示所有字符。如果我们再次按下它,将出现 100 个字符。我该怎么做?

【问题讨论】:

标签: javascript c# html css asp.net


【解决方案1】:

您可以使用事件侦听器并将父级设置为全文,就像单击按钮时一样

const fullText = 'Breaking India: Western Interventions in Dravidian and Dalit Faultlines is a book written by Rajiv Malhotra and Aravindan Neelakandan which argues that India\'s integrity is being undermined and some more'

// get the more buttons
const buttons = document.getElementsByClassName('More');

// attach an click event listener
Object.values(buttons).forEach(button => {
  button.addEventListener('click', (event) => {
  
    // onclick set the parents content to the fullText
    event.target.parentNode.textContent = fullText
  })
})
<span class="p1">
                    Breaking India: Western Interventions in Dravidian and Dalit Faultlines is a book written by Rajiv Malhotra and Aravindan Neelakandan which argues that India's integrity is being undermined
                    <button class="More">..Show More</button>
                </span>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2015-06-22
    • 1970-01-01
    相关资源
    最近更新 更多