jquery 阅读更多和更少切换

标签 jquery

当我点击“更多”时,它可以工作,但屏幕位于顶部。最后还显示“关闭”。再次单击“关闭”后,它仍然保持“关闭”状态。我希望它回到“更多”。我怎样才能做到这一点?

<p class="disp-cont">
    Several Australian home owners have been utilizing the services of mortgage brokers to fulfill their dreams of owning a house at an affordable rate.
</p>
<p class="more-cont" style="display:none;">
    Whether you are aspiring to buy a new house or considering upgrading your existing home loan, an expert mortgage broker can help you achieve your objective in the simplest and easiest way! For the uninitiated, here’s a sneak peek on <strong>what is mortgage broking?</strong><br>
    Mortgage broking involves the act of intermediating between the borrower and the lender (bank & non-bank). While on one hand, the loan specialist assists the borrower to qualify for a mortgage, on the other hand, he negotiates with the lender on the borrower’s behalf. The role of mortgage broker starts right from the time when you plan to explore your options of financing/refinancing till the deal is finalized. 
</p>
<a href="#" class="more">more</a>
$('.more').click(function() {
    $(this).prev('.more-cont').slideToggle();
    $(this).html('close');
});

最佳答案

要阻止滚动位置移动到页面顶部,请在传递给点击处理程序的事件上调用 preventDefault()。要切换文本,您可以向 text() 提供一个函数,该函数根据当前文本设置值。试试这个:

$('.more').click(function(e) {
  e.preventDefault();
  $(this).text(function(i, t) {
    return t == 'close' ? 'more' : 'close';
  }).prev('.more-cont').slideToggle()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="disp-cont">
  Several Australian home owners have been utilizing the services of mortgage brokers to fulfill their dreams of owning a house at an affordable rate.
</p>
<p class="more-cont" style="display:none;">
  Whether you are aspiring to buy a new house or considering upgrading your existing home loan, an expert mortgage broker can help you achieve your objective in the simplest and easiest way! For the uninitiated, here’s a sneak peek on <strong>what is mortgage broking?</strong>
  <br>Mortgage broking involves the act of intermediating between the borrower and the lender (bank & non-bank). While on one hand, the loan specialist assists the borrower to qualify for a mortgage, on the other hand, he negotiates with the lender on the
  borrower’s behalf. The role of mortgage broker starts right from the time when you plan to explore your options of financing/refinancing till the deal is finalized.
</p>
<a href="#" class="more">more</a>

<p class="disp-cont">
  Several Australian home owners have been utilizing the services of mortgage brokers to fulfill their dreams of owning a house at an affordable rate.
</p>
<p class="more-cont" style="display:none;">
  Whether you are aspiring to buy a new house or considering upgrading your existing home loan, an expert mortgage broker can help you achieve your objective in the simplest and easiest way! For the uninitiated, here’s a sneak peek on <strong>what is mortgage broking?</strong>
  <br>Mortgage broking involves the act of intermediating between the borrower and the lender (bank & non-bank). While on one hand, the loan specialist assists the borrower to qualify for a mortgage, on the other hand, he negotiates with the lender on the
  borrower’s behalf. The role of mortgage broker starts right from the time when you plan to explore your options of financing/refinancing till the deal is finalized.
</p>
<a href="#" class="more">more</a>

关于jquery 阅读更多和更少切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40040741/

相关文章:

JavaScript 未捕获引用错误 : ListMgr is not defined

javascript - "$"符号在 jQuery 或 JavaScript 中意味着什么?

javascript - 子字符串不适用于 jquery .text()

jquery - 在单个输入字段中选择多个日期?

c# - jQuery Ajax、MVC 和查询字符串

jquery - DataTables - 不使用 Ajax 扩展子级详细信息

javascript - 需要在 jQuery 中获取元素的文本值

javascript - 如何使用ajax jquery将图像上传到服务器?

jquery - JavaScript 中的 DoEvents

javascript - Kendo UI ListView - 在编辑时切换模型实例