您好,欢迎访问一九零五行业门户网

jQuery自定义框并获取框内数据

这次给大家带来jquery自定义框并获取框内数据,jquery自定义框并获取框内数据的注意事项有哪些,下面就是实战案例,一起来看一下。
jquery库:
jquery -1.10.2.min.js,jquery ui - v1.12.1。
jquery 代码
不多说了,之间上代码。不懂的地方看注释。
<script type="text/javascript">  //鼠标按下时的x y坐标  varmousedownx;  varmousedowny;  //鼠标按下时移动的x y 坐标  varmousemovex;  varmousemovey;  //移动的状态  varismove =false;  /*初始化 选择框 */  functioninit() {   $(#selected).css(display,none);   $(#selected).css(top,0);   $(#selected).css(left,0);   $(#selected).css(width,0);   $(#selected).css(height,0);  }  $(document).ready(function() {   init();   varselectedtd =newarray();//创建被选中表格数组   vartd = $(td);//获取所有表格信息   for(vari = 0; i < td.length; i++) { selectedtd.push(td[i]); } $("#tablep").mousedown(function(event) { mousedownx = event.clientx - $(this).offset().left;; mousedowny = event.clienty - $(this).offset().top; console.log("mousedownx="+ mousedownx +" mousedowny="+ mousedowny ); if(event.target.id.match(/selected/)) { ismove =true; } //鼠标按下并移动时进行判断(拖拽 or 画框) $("#tablep").mousemove(function(event) { mousemovex = event.clientx - $(this).offset().left; mousemovey = event.clienty - $(this).offset().top; varselectp = document.getelementbyid("selected"); if(ismove) { //拖拽的代码,因为实在不想算 xy 了,所以使用了jquery ui $("#selected").draggable(); //这部分是负责画框的时候,实时把框住的表格变色的,(代码和下面的代码重复了) varleft = selectp.offsetleft, top = selectp.offsettop; width = selectp.offsetwidth, height = selectp.offsetheight; for(vari = 0; i < selectedtd.length; i++) { varsl = selectedtd[i].offsetwidth + selectedtd[i].offsetleft; varst = selectedtd[i].offsetheight + selectedtd[i].offsettop; if(sl > left && st > top && selectedtd[i].offsetleft < left + width && selectedtd[i].offsettop < top + height) { if(selectedtd[i].classname.indexof("selected") == -1) { selectedtd[i].classname ="selected"; } }else{ if(selectedtd[i].classname.indexof("selected") != -1) { selectedtd[i].classname ="td"; } } } }else{ //重复的代码,完了再把它抽取出来 varleft = selectp.offsetleft, top = selectp.offsettop; width = selectp.offsetwidth, height = selectp.offsetheight; for(vari = 0; i < selectedtd.length; i++) { varsl = selectedtd[i].offsetwidth + selectedtd[i].offsetleft; varst = selectedtd[i].offsetheight + selectedtd[i].offsettop; if(sl > left && st > top && selectedtd[i].offsetleft < left + width && selectedtd[i].offsettop < top + height) { if(selectedtd[i].classname.indexof("selected") == -1) { selectedtd[i].classname ="selected"; } }else{ if(selectedtd[i].classname.indexof("selected") != -1) { selectedtd[i].classname ="td"; } } } //鼠标抬起结束画框,和拖动 $("#tablep").mouseup(function() { console.log("mouseupx="+ mousemovex +" mouseupy="+ mousemovex); ismove =false; $(this).unbind('mousemove'); }) //画框 displayselected(mousedowny, mousedownx, mousemovex, mousemovey); } }) }) //当鼠标在已经画好的框上时,改变鼠标指针样式,就是十字形了 $("#selected").mouseenter(function() { $("#selected").css("cursor","move"); }); }); functiondisplayselected(mousedowny, mousedownx, mouseupx, mouseupy) { $("#selected").css("display","block"); $("#selected").css("top", mousedowny); $("#selected").css("left", mousedownx); varmovex = mousemovex - mousedownx; varmovey = mousemovey - mousedowny; if(movey < 0) { $("#selected").css("top", event.clienty - $("#table").offset().top); } if(movex < 0) { $("#selected").css("left", event.clientx - $("#table").offset().left); } $("#selected").css("width", math.abs(movex)); $("#selected").css("height", math.abs(movey)); } </script>
测试用的html
使用table进行的测试:
<p id="tablep"style="width: 1500px; height: 1500px;top: 100px; left:100px; position: absolute;">   <p id="selected"style="border:5px dotted rgb(239, 37, 17);position: absolute;display: none;"></p>   <table border="1"style=" width: 1500px; height: 1500px;"id="table">   <tr>    <td id="1"class="td"></td>    <td id="2"class="td"></td>    <td id="3"class="td"></td>    <td id="4"class="td"></td>    <td id="5"class="td"></td>    <td id="6"class="td"></td>   </tr>   <tr>    <td id="7"class="td"></td>    <td id="8"class="td"></td>    <td id="9"class="td"></td>    <td id="10"class="td"></td>    <td id="11"class="td"></td>    <td id="12"class="td"></td>   </tr>   <tr>    <td id="1"class="td"></td>    <td id="2"class="td"></td>    <td id="3"class="td"></td>    <td id="4"class="td"></td>    <td id="5"class="td"></td>    <td id="6"class="td"></td>   </tr>   </table>  <!--表格代码太多所以...-->  </p>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vuex+actions使用详解
jquery实现计时器功能详解
js如何设置禁止表单重复提交
以上就是jquery自定义框并获取框内数据的详细内容。
其它类似信息

推荐信息