前端的几个技巧

一、文字输入回车转换为--<br />
var text = $('.wra').html();
text=text.replace(/[\n\r]/g,'<br>')
$('.wra').html(text);

二、手机端跳转
onclick="location.href ='/html/html.html?doctor=' + doctorId"

三、移除点击事件
$(".apply").attr("onclick","apply();"); attr("事件","方法名")
$(".apply").removeAttr("onclick");

四、超出隐藏--选择几行:
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
---单行---
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

五、正则
/^(?=.*\d)[a-z\d]{6,20}$/i 判断 6到20字符

六、判断网络
if(window.navigator.onLine==true) { 
    alert("首次 -- 已连接"); 这个可以,但是没有网络怎样加载js啊
  }else { 
    alert("首次 -- 未连接");
}

七、判断后台返回img错误调用img(图片)
onerror="nofind();"
function nofind(){
var img=event.srcElement;
img.src="/images/onerror.png";
img.onerror=null; //控制不要一直跳动
}
<img src="onerror.png" alt="" class="surface_plot" onerror="src='onerror.png'" />

八、判断val值为空
$(function() {
$(".searchbox").blur(function() {
if ($(".searchbox").val() == ""){
$(".searchbox").focus();
}else
alert($(".searchbox").val());
});
});

九、要检查是否是可见的元素,你可以使用下面的代码片段:
if($(element).is(":visible") == "true")
{
//可见
}
else
{
//不可见
}

十、设置计时器
$(document).ready(function(){

window.setTimeout(function(){
// 执行代码
}, 500);

});
---------------------

原文:https://blog.csdn.net/qq_38881495/article/details/81211715

猜你喜欢

转载自www.cnblogs.com/xdanny/p/11288895.html