例如,我有一个包含所有文本的 div,例如:
<div class="text-container">
<p>
Though we welcome the idea of a smaller connector, we're miffed that Apple couldn't just adopt the semi-industry standard of Micro-USB. That would make things easier for smartphone users across the globe. Yet, even so, the smaller connector may be a smart move for the future. The 30-pin connector has been around since 2003, long before the iPhone even existed: frankly, it's a dust magnet. A smaller connector helps shave extra space to achieve a smaller phone with perhaps a bigger battery. The new connector cable will mainly be used for syncing and charging by most people who own an Apple TV or Bluetooth/AirPlay accessories.
</p>
</div>
我想创建这样的东西:
<div class="text-container">
<p>
Though we welcome the idea of a smaller connector...<a href="some-javascript-to-show-more">[show more]</a>
</p>
</div>
我想我应该获取 div 的所有内容,然后找到第一个例如 50 个字符并在那里放置一个链接,所有其他文本都放在某个将被隐藏的 div 中,然后单击链接后显示其他内容.
它应该是类似切换的,如果它被展开,则将文本从 [显示更多] 更改为 [显示更少],反之亦然。
任何建议如何使用纯 javascript 和 jquery 本身而不使用其他 jQuery 插件来实现这一点?