JS 基础
1.JS给span赋值
<span id="textshow" style="font-size:19px;color:#ff0000;"></span>
document.getElementById("textshow").innerText = PicNmae;
2.JS截取字符串
原数据: 2017-01-18 09:53:00
JSON.stringify(selectdata.f_ReadDate).substring(1, 11)
截取后:
3.JS通过ajax判断文件是否存在
1 $.ajax({ 2 url: picSrc, //Path 3 async: false, 4 type: 'HEAD', 5 error: function () { 6 $("#imgshow").attr("src", "/Content/img/NoExit.jpg"); //不存在显示默认值 7 }, 8 success: function () { 9 $("#imgshow").attr("src", "/UserPicture/" + Day.replace("-", "").replace("-", "") + "/" + PicNmae); //有抓拍图片 10 } 11 });
4.绑定下级元素的点击事件
1 <div class="listview set-border padding10" data-role="listview" data-on-list-click="list.find('.list-title').text()" id="xxx"> 2 @{ 3 foreach (var x in ViewBag.dr) 4 { 5 <div class="list"> 6 <img src="~/Content/Image/folder-documents.png" class="list-icon"> 7 <span class="list-title">@x.f_ControllerID-@x.f_DoorName</span> 8 </div> 9 } 10 } 11 </div>
$("#xxx .list").on("click", function () { alert("1111") })
5.回车等于点击登陆
document.onkeydown = function (e) { if (!e) e = window.event; if ((e.keyCode || e.which) == 13) { document.getElementById("Login").focus(); //document.getElementById("Login").click(); } }
6.在指定元素下面添加元素
$("#aa").after("<button type="button">删除</button>);
7.删除指定元素
$(id).remove();
8.指定元素添加style
var span = document.getElementById(SpanID); span.style.width = "200px"
9.js拼接Html的onclick传参无效
错误:
var ALLID = Count + "All"; <button id='" + DelID + "' onclick='DelDiv(" + ALLID + ")' class='btn btn-lg btn-default' style='width:90%' type='button' >删除</button>
解决:
onclick='DelDiv(" + JSON.stringify(ALLID) + ")'
10.获取指定ID或者元素下的指定元素个数
var x = $('#Field').children('div').length;