HTML页面自动重新加载
HTML page reloads automatically
我的HTML页面如下:
$(document).ready(function() {
var showChar = 380;
var ellipsestext = "...";
var moretext = "Read more";
var lesstext = "Read less";
var lesshtml="";
var morehtml="";
var content = $(".more").html();
if(content.length > showChar) {
var lastChar = content.charAt(showChar);
if(lastChar == '/'){
showChar = showChar+1;
}
var c = content.substr(0, showChar);
var h = content.substr(showChar-1, content.length - showChar);
var lesshtml = c + '<span class="moreelipses">'+ellipsestext+' <a href="" class="morelink">'+moretext+'</a></span>';
var morehtml = content + '<span><a href="" class="morelink">'+lesstext+'</a></span>';
$(".more").html(lesshtml);
}
$(".morelink").click(function(){
if ($(this).text()==moretext){
$(".more").html(morehtml);
}else{
$(".more").html(lesshtml);
}
return false;
});
});
a {
color: #EA7813
}
a:visited {
color: #EA7813
}
a.morelink {
text-decoration:none;
outline: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="more">
<p>Can't reach the light switch when you walk into a room? Need one added near the door? If you would like to have a switch professionally installed in a location where no switch currently exists, one of our vetted, <strong>local electrical professionals</strong> will take care of your <strong>light switch installation </strong>and make sure it's working smoothly.</p>
<p><strong>What's included:</strong></p>
<ul>
<li>Install one (1) new gang box and associated wiring extension from existing circuit (up to 25')</li>
<li>Install and pair one (1) <strong>customer-supplied</strong> smart light switch in a new location <strong>-OR-</strong> provide and install one (1) standard single-pole switch and cover plate</li>
<li>Ensure all features are operating to maximum performance</li>
<li>Demonstrate basic device features</li>
<li>90-day labor warranty</li>
</ul>
<p><strong>Out of scope:</strong></p>
<ul>
<li>Smart switch not included, price includes installation only of smart switch -OR- providing and installing a standard switch</li>
<li>Requires existing, accessible electrical circuit within 25'</li>
<li>Drywall patching & painting</li>
<li>Requires existing, compatible network</li>
<li>No diagnostic, repair or parts are included with this service</li>
<li>No non-stock parts, materials or system repair / upgrades are included with this service</li>
</ul>
</div>
我想要的是扩大和缩小主体。扩展工作正常,但当我收缩,页面重新加载。我想停止重新装填。在我的click函数中,永远不会触发else部分。相反,页面会重新加载。我想停止重新加载并运行else中的代码,以便正文适当收缩。
try this
$(document).on('click', '.morelink', function(){
if ($(this).text()==moretext){
$(".more").html(morehtml);
} else {
$(".more").html(lesshtml);
}
return false;
});
委托活动
$('body').on('click','.morelink',function(e){
e.preventDefault();
//other code
});
代替在href中使用#
,您可以在href中使用javascript:;
,这将不会让url更改
var lesshtml = c + '<span class="moreelipses">'+ellipsestext+' <a href="javascript:;" class="morelink">'+moretext+'</a></span>';
var morehtml = content + '<span><a href="javascript:;" class="morelink">'+lesstext+'</a></span>';
$(".more").html(lesshtml);
}
相关文章:
- 在新的浏览器选项卡上加载新的aspx页面后调用JavaScript函数
- 更改css链接并等待加载新的css
- 如何在Ajax加载新内容时停止JavaScript执行
- 当在ng视图中加载新路由时,Tabbing(tabindex)针对错误的元素
- 如何使用输入数据加载新的extjs图表
- JQuery Masonry.Ajax图标和“;加载新项目”;绳子放错地方了
- 通过javascript在同一个打开的选项卡中重新加载新的网页内容
- 当用户到达页面右侧时加载新内容
- 数据表 AJAX 筛选器重新加载数据
- 超链接单击以加载新页面并执行JavaScript
- 单击时加载新的 SVG
- 在RadWindow中加载新页面,然后从加载的窗体中关闭窗口
- ajax在页面刷新之前不会加载新的sql结果
- 谷歌地图API-在从ajax页面加载新数据之前清除标记、多段线
- 使用Ajax加载页面,并在必要时加载新脚本
- 加载新页面时的过渡效果,AJAX
- Ajax没有正确加载新页面
- 正在加载新的Javascript,但正在调用旧的Javascript.MVC
- 咕噜咕噜的手表:实时加载重新加载落后 1 步....
- 我怎么能有一个花哨的盒子一样的弹出体加载,但与cookie不连续加载重新加载