我有一个包含多行和多列的HTML表格。我也有一个名为userRowIndex的变量,它具有保存在其中的特定行的索引。我希望能够使用JavaScript和我的userRowIndex变量来使整行内容可编辑。我怎样才能做到这一点?我可以使用Javascript通过rowIndex查找表格行吗?
-2
A
回答
1
var rows = document.getElementById("myTable").getElementsByTagName('tr');
// whatever your userRowIndex is :
var userRowIndex = 1;
rows[userRowIndex].className = 'red';
<html>
<head>
<style type='text/css'> .red{color:red;}</style>
</head>
<body>
<table id="myTable">
\t <tr>
\t \t <td>1</td>
\t \t <td>One</td>
\t </tr>
\t <tr>
\t \t <td>2</td>
\t \t <td>Two</td>
\t </tr>
\t <tr>
\t \t <td>3</td>
\t \t <td>Three</td>
\t </tr>
</table>
</body>
</html>
+0
这种方法很好,但是在很长的桌子上它可能会造成滞后 –
0
这是一个基本的例子,如何在特定td
可以使用rowIndex
// get the table
var getTable = document.querySelector("#demotable");
// adding event listener
getTable.addEventListener('click', function(event) {
// current cell index
var col = event.target.cellIndex,
// get index of row from td
row = event.target.parentNode.rowIndex;
// set the contenteditable property to it
this.rows[row].cells[col].setAttribute('contenteditable', 'true')
})
<table border id="demotable">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
还要注意一个td
可以在编辑来为可编辑即使t他整个行可以做编辑,但只有一个单元可以同时
0
这可能是改变更快:
function getRowByIndex(userRowIndex)
{
var rows = null;
var row = null;
try
{
rows = document.getElementById("myTable").getElementsByTagName('tr');
if(rows!=null)
{
// This uses 0 based indexing, so assume that the passed value is in that range
row = rows[userRowIndex];
}
}
catch(e)
{
alert("getRowByIndex Error: " + e.Message);
}
finally
{
}
return row;
}
相关问题
- 1. 我可以通过表格URL执行Google App脚本吗?
- 2. XAML:我可以通过名称引用网格行或列吗?
- 3. 可以使用列表查找查询中的行集合吗?
- 4. 我可以鼓励使用表格单元格来换行吗?
- 5. 我可以通过JavaScript在运行时
- 6. 的rowIndex使用Javascript
- 7. 我可以通过纯JavaScript
- 8. 我可以通过Javascript向表单添加参数吗?
- 9. 使用Windows.Automation,我可以通过正则表达式找到AutomationElement吗?
- 10. 我可以通过selenium使用firefox ide运行powershell脚本吗?
- 11. 我可以只使用关键字段来检查等于吗?我可以通过子类使用等于吗?
- 12. TrueZip可以用于通过流查找
- 13. 我可以使用fancybox进行在线表格联系吗?
- 14. 我可以从JavaScript查找用户代理吗?
- 15. 屏蔽ui网格如何通过sui-updated-marker找到rowIndex
- 16. 有人可以通过这个JavaScript走过我吗?
- 17. 我可以在JavaScript中运行JavaScript吗?
- 18. JavaScript可以被过度使用吗?
- 19. 通过使用通用映像,我可以通过docker节省内存吗?
- 20. 使用Javascript GridView的Rowindex
- 21. 我们可以通过chrome在多单元格表格中解决表格行背景图像问题吗?
- 22. 我可以通过编程检查Google BigQuery查询成本吗?
- 23. 我可以使用EMACS for Javascript吗?
- 24. 我可以在JavaScript中使用php吗?
- 25. 我可以在Javascript中使用SendKeys吗?
- 26. 我可以从JavaScript使用NSSpeechSynthesizer吗?
- 27. 可以通过苹果appstore审查吗?
- 28. 完全可以通过JavaScript使用cocos2d吗?
- 29. (OpenID)我可以使用ClaimedIdentifier查找用户吗?
- 30. 我可以使用ListUsers API通过用户的uuid查询Cognito用户池吗?
您使用的是JavaScript库? –
不,我没有使用JS库,我也不太喜欢使用其中一个。 –
你可以在'nnth-CSS'选择器中使用'document.querySelector'来实现这个功能。 – Amy