[英]How to disable and enable HTML table using Javascript?
我想知道如何通過單擊html按鈕使用Javascript禁用和啟用HTML表格上的突出顯示。
這是我的代碼:
tabletest.html
<html>
<head>
<script type="text/javascript">
function disableTable() {
document.getElementbyId('tblTest').disabled = true;
}
function enableTable() {
document.getElementbyId('tblTest').disabled = false;
}
</script>
<style type="text/css">
table#tblTest {
width: 100%;
margin-top: 10px;
font-family: verdana,arial,sans-serif;
font-size:12px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table#tblTest tr.highlight td {
background-color: #8888ff;
}
table#tblTest tr.normal {
background-color: #ffffff;
}
table#tblTest th {
white-space: nowrap;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table#tblTest td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
</head>
<body>
<table id="tblTest">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" >
<td>Tom</td>
<td>UK </td>
</tr>
<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" >
<td>Hans</td>
<td>Germany</td>
</tr>
<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" >
<td>Henrik</td>
<td>Denmark</td>
</tr>
<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" >
<td>Lionel</td>
<td>Italy</td>
</tr>
<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" >
<td>Ricardo</td>
<td>Brazil</td>
</tr>
<tr onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'" >
<td>Cristiano</td>
<td>Portugal</td>
</tr>
</tbody>
</table>
<input type="button" onclick="disableTable();" value="Disable" />
<input type="button" onclick="enableTable()" value="Enable" />
</body>
</html>
每當我單擊“ Disable
按鈕時,表格突出顯示仍處於啟用狀態。 我對此有點新意,所以我真的需要你的幫助。
如果要將其“查看”禁用或啟用,請將類規則添加到樣式表,並將類添加到表中以啟用或禁用。
function disableTable() {
addClassName(document.getElementbyId('tblTest'), 'disabled');
}
function enableTable() {
removeClassName(document.getElementbyId('tblTest'), 'disabled');
}
function trim(s) {
return s.replace(/(^\s+)|(\s+$)/g,'').replace(/\s+/g,' ');
}
function hasClassName (el, cName) {
var re = new RegExp('(^|\\s+)' + cName + '(\\s+|$)');
return el && re.test(el.className);
}
function addClassName(el, cName) {
if (!hasClassName(el, cName)) {
el.className = trim(el.className + ' ' + cName);
}
}
function removeClassName(el, cName) {
if (hasClassName(el, cName)) {
var re = new RegExp('(^|\\s+)' + cName + '(\\s+|$)','g');
el.className = trim(el.className.replace(re, ''));
}
}
這將從你的tr中刪除onmouseover事件。
function disableTable() {
var rows = document.getElementsByTagName("tr");
for (var i = 0; i < rows.length; i++) {
rows[i].onmouseover= null;
}
}
<html>
<head>
<script type="text/javascript">
disable = new Boolean();
function highlight(a) {
if(disable==false)a.className='highlight'
}
function normal(a) {
a.className='normal'
}
</script>
<style type="text/css">
table#tblTest {
width: 100%;
margin-top: 10px;
font-family: verdana,arial,sans-serif;
font-size:12px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table#tblTest tr.highlight td {
background-color: #8888ff;
}
table#tblTest tr.normal {
background-color: #ffffff;
}
table#tblTest th {
white-space: nowrap;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table#tblTest td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
</head>
<body>
<table id="tblTest">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr onMouseOver="highlight(this)" onMouseOut="normal(this)" >
<td>Tom</td>
<td>UK </td>
</tr>
<tr onMouseOver="highlight(this)" onMouseOut="normal(this)" >
<td>Hans</td>
<td>Germany</td>
</tr>
<tr onMouseOver="highlight(this)" onMouseOut="normal(this)" >
<td>Henrik</td>
<td>Denmark</td>
</tr>
<tr onMouseOver="highlight(this)" onMouseOut="normal(this)" >
<td>Lionel</td>
<td>Italy</td>
</tr>
<tr onMouseOver="highlight(this)" onMouseOut="normal(this)" >
<td>Ricardo</td>
<td>Brazil</td>
</tr>
<tr onMouseOver="highlight(this)" onMouseOut="normal(this)" >
<td>Cristiano</td>
<td>Portugal</td>
</tr>
</tbody>
</table>
<input type="button" onclick="disable = true;" value="Disable" />
<input type="button" onclick="disable = false;" value="Enable" />
</body>
</html>
修復了你的代碼。 使用函數檢查它是否被禁用,如果不是,則突出顯示。 如果是,那就不要。 很簡單。
無法禁用表格。 你想要做的是禁用輸入按鈕,並在HTML表格上有一個類,它會給你選擇按鈕的onclick事件淡出/淡出的幻覺。
請遵循以下食譜:
定義兩組CSS規則。 一組規則始終以table.enabled
,另一組以table.disabled
要啟用/禁用整個表,請找到DOM元素(當表具有id
tblTest
時使用document.getElementbyId('tblTest')
)並分配相應的類:
document.getElementbyId('tblTest').classname = enabled ? 'enabled' : 'disabled';
如果你想讓它在任何地方“無法點擊” - 你可以在上面添加一個相同大小的transperent div。
<html>
<script>
function disableTable(){
document.getElementById("myTableFieldSet").disabled = true;
}
function enableTable(){
document.getElementById("myTableFieldSet").disabled = false;
}
</script>
<body>
<form>
<fieldset>
<!-- place the table in a separate fieldset -->
<fieldset id="myTableFieldSet">
<table id="myTable">
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<tr>
<td><input type="text"></td>
<td><input type="email"></td>
</tr>
</table>
</fieldset>
</fieldset>
<button type="button" onclick="disableTable()">Disable Table</button>
<button type="button" onclick="enableTable()">Enable Table</button>
</form>
</body>
</html>
通過將表保留在“fieldset”標記中並通過Javascript禁用它,可以阻止包含輸入字段的整個表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.