Javascript/HTML的几个问题(ContentEditable,Selection and cursor style)

今天在解决一个问题时用到的几个Javascript/Html的知识点,简单总结一下:

 

一、元素的ContentEditable属性

        在Html中,可以将元素的contentEditable属性设置为true,这样就可以由用户来编辑这个控件的大小及内容,例如:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title>新建网页 3</title>
  5. </head>
  6. <body>
  7.     
  8.     <div contentEditable="true">
  9.         <span contentEditable = "true">This span is contentEditable</span>
  10.     </div>
  11. </body>
  12. </html>

      如果要阻止选择可编辑的元素,即不让周围出现可控制的控制点,但仍然可以显示四边箭头的光标,则可以在onControlSelect事件中来阻止:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title>新建网页 3</title>
  5. </head>
  6. <body>
  7.     
  8.     <div contentEditable="true">
  9.         <span contentEditable = "true" oncontrolselect="javascript:return false">This span is contentEditable</span>
  10.     </div>
  11. </body>
  12. </html>

二、关于document.selection

      1、Get Selection

            可以通过Javascript获取当前页面选择的文字或控件,大致代码如下:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title>新建网页 1</title>
  5. </head>
  6. <body>
  7.     <span> This is a Span </span>
  8.     <div> This is a div </div>
  9.     <table width="140" height="40" border="1">
  10.         <tr>
  11.             <td>a</td>
  12.             <td>c</td>
  13.         </tr>
  14.         <tr>
  15.             <td>b</td>
  16.             <td>d</td>
  17.         </tr>
  18.     </table>
  19.     <hr>
  20.     <img src="" width="30" height="30">
  21.     <input type=button οnclick="getSelectText()" value="Get Selected Text" >
  22.     <script>
  23.         function getSelectText()
  24.         {
  25.             if( document.selection.type!="none" )
  26.             {
  27.                 //alert( document.selection.createRange().text )
  28.                 alert( document.selection.createRange().htmlText )
  29.             }
  30.         }
  31.     </script>
  32. </body>
  33. </html>

     2、Set Selection

          利用Range的select()来选择指定的文字或元素。如下例子:

  1. <input id="txb" type="text" value="Text Box"/> 
  2. <a href="#" onclick="document.getElementById('txb').select()">Select</a>
  1. <span id="spn">this is a span.</span>
  2. <a href="#" onclick="SelectText();">Select</a>
  3. <script language="javascript">
  4. function SelectText()
  5. {
  6.     var range = document.body.createTextRange();
  7.     range.findText("this is a span.");
  8.     range.select();
  9. }
  10. </script>

 

  1. <select id="slt1"><option>select</option></select>
  2. <select id="slt2"><option>select</option></select>
  3. <a href="#" onclick="SelectControl();">Select</a>
  4. <script language="javascript">
  5. function SelectControl()
  6. {
  7.     var controlRange = document.body.createControlRange();
  8.     controlRange.add(document.getElementById('slt1'));
  9.     controlRange.add(document.getElementById('slt2'));
  10.     controlRange.select();
  11. }
  12. </script>

三、光标(鼠标/cursor)样式:

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title>新建网页 1</title>
  5. </head>
  6. <body>
  7.     
  8.     <table width="100%" cellpadding="0" cellspacing="0" border="1">
  9.         <tr><td align="center">光标/鼠标样式</td></tr>
  10.         <tr><td style="cursor:auto">cursor:auto</td></tr>
  11.         <tr><td style="cursor:crosshair">cursor:crosshair</td></tr>
  12.         <tr><td style="cursor:pointer">cursor:pointer</td></tr>
  13.         <tr><td style="cursor:hand">cursor:hand</td></tr>
  14.         <tr><td style="cursor:wait">cursor:wait</td></tr>
  15.         <tr><td style="cursor:help">cursor:help</td></tr>
  16.         <tr><td style="cursor:no-drop">cursor:no-drop</td></tr>
  17.         <tr><td style="cursor:text">cursor:text</td></tr>
  18.         <tr><td style="cursor:move">cursor:move</td></tr>
  19.         <tr><td style="cursor:n-resize">cursor:n-resize</td></tr>
  20.         <tr><td style="cursor:s-resize">cursor:s-resize</td></tr>
  21.         <tr><td style="cursor:e-resize">cursor:e-resize</td></tr>
  22.         <tr><td style="cursor:w-resize">cursor:w-resize</td></tr>
  23.         <tr><td style="cursor:ne-resize">cursor:ne-resize</td></tr>
  24.         <tr><td style="cursor:nw-resize">cursor:nw-resize</td></tr>
  25.         <tr><td style="cursor:se-resize">cursor:se-resize</td></tr>
  26.         <tr><td style="cursor:sw-resize">cursor:sw-resize</td></tr>
  27.         <tr><td style="cursor:not-allowed">cursor:not-allowed</td></tr>
  28.         <tr><td style="cursor:progress">cursor:progress</td></tr>
  29.         <tr><td style="cursor:default">cursor:default</td></tr>
  30.         <tr><td style="cursor:auto">用户自定义(可用动画)  cursor: url(' # ');  # = 光标文件地址   (注意文件格式必须为:.cur 或 .ani)。</td></tr>
  31.     </table>
  32.     
  33. </body>
  34. </html>
table { border-collapse: collapse; width: 100%; } td, th { border: 1.5px solid black; padding: 5px; } table img { max-width: 150px; height: auto; } <table id="myTable"> <thead class="sticky-header"> <tr> <td contenteditable="true"><input type="text" oninput="filterTable(0)" placeholder="工艺膜层"></td> <td contenteditable="true"><input type="text" oninput="filterTable(1)" placeholder="AOI Step"></td> <td contenteditable="true"><input type="text" oninput="filterTable(2)" placeholder="不良类型"></td> <td contenteditable="true"><input type="text" oninput="filterTable(3)" placeholder="Layer(Code)"></td> <td contenteditable="true"><input type="text" oninput="filterTable(4)" placeholder="Type"></td> <td contenteditable="true"><input type="text" oninput="filterTable(5)" placeholder="Dpet"></td> <td contenteditable="true"><input type="text" oninput="filterTable(6)" placeholder="Subcode"></td> <td contenteditable="true">Code描述</td> <td contenteditable="true">Image1</td> <td contenteditable="true">Image2</td> <td contenteditable="true">Image3</td> <td contenteditable="true">Image4</td> <td contenteditable="true">Image5</td> <td contenteditable="true">Image6</td> <td contenteditable="true">判定细则</td> </tr> </thead> <tbody> <tr> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;" > </td> <td> <input type="file" onchange="previewImage(this)"onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)"onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td contenteditable="true">Particle</td> </tr> </tbody> </table>这个表格怎么设置自定义调整列宽
最新发布
07-16
要设置自定义调整列宽,你可以使用CSS中的`table-layout`属性和`width`属性来实现。具体步骤如下: 1. 在CSS样式中,将`table-layout`属性设置为`fixed`。这将使表格的列宽度固定。 2. 为每个表格单元格(`td`和`th`)设置一个固定的宽度(或者使用百分比),以确定每列的默认宽度。 3. 在需要调整列宽的地方,添加一个可调整宽度的元素,比如一个拖动条。 4. 使用JavaScript来处理拖动条的事件,并根据拖动条的位置调整表格列的宽度。 以下是一个示例代码段,演示了如何实现自定义调整列宽的功能: ```html <!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; width: 100%; table-layout: fixed; } td, th { border: 1.5px solid black; padding: 5px; } th { background-color: #f2f2f2; } .column-resizer { background-color: #ddd; width: 6px; height: 100%; position: absolute; right: 0; top: 0; cursor: col-resize; } </style> </head> <body> <table id="myTable"> <thead class="sticky-header"> <tr> <th contenteditable="true" style="width: 150px">工艺膜层</th> <th contenteditable="true" style="width: 150px">AOI Step</th> <th contenteditable="true" style="width: 150px">不良类型</th> <th contenteditable="true" style="width: 150px">Layer(Code)</th> <th contenteditable="true" style="width: 150px">Type</th> <th contenteditable="true" style="width: 150px">Dpet</th> <th contenteditable="true" style="width: 150px">Subcode</th> <th contenteditable="true" style="width: 150px">Code描述</th> <th contenteditable="true" style="width: 150px">Image1</th> <th contenteditable="true" style="width: 150px">Image2</th> <th contenteditable="true" style="width: 150px">Image3</th> <th contenteditable="true" style="width: 150px">Image4</th> <th contenteditable="true" style="width: 150px">Image5</th> <th contenteditable="true" style="width: 150px">Image6</th> <th contenteditable="true" style="width: 150px">判定细则</th> </tr> </thead> <tbody> <tr> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td contenteditable="true">Particle</td> </tr> </tbody> </table> <script> var columnResizer = document.createElement('div'); columnResizer.className = 'column-resizer'; document.addEventListener('DOMContentLoaded', function() { var table = document.getElementById('myTable'); var resizableColumns = table.querySelectorAll('th'); resizableColumns.forEach(function(column) { var resizer = columnResizer.cloneNode(true); column.appendChild(resizer); resizer.addEventListener('mousedown', function(e) { var originalWidth = column.offsetWidth; var startX = e.pageX; document.addEventListener('mousemove', resizeColumn); document.addEventListener('mouseup', stopResize); function resizeColumn(e) { var width = originalWidth + (e.pageX - startX); column.style.width = width + 'px'; } function stopResize() { document.removeEventListener('mousemove', resizeColumn); document.removeEventListener('mouseup', stopResize); } }); }); }); </script> </body> </html> ``` 在这个示例中,每个表头单元格都添加了一个可调整宽度的元素`column-resizer`,当你点击并拖动该元素时,可以调整相应列的宽度。你可以根据需要修改其中的样式和事件处理程序来满足你的要求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值