我有一个用动态数据创建的HTML表,无法预测其中的行数。我想要做的是在单击特定行的最后一个单元格时获取行单元格的值。
答案 0 :(得分:0)
使用计数器变量计算行号。
答案 1 :(得分:0)
使用jquery以简洁和兼容:
$('tr td:nth-child(4)').on('click', function(evt){
console.log($(this).parent());
});
它为您提供刚刚单击的DOM对象。如果你只想让最后一个TD触发事件(假设你有4列),它可能看起来更像这样:
Collecting pyfst
Using cached pyfst-0.2.3.tar.gz
Installing collected packages: pyfst
Running setup.py install for pyfst: started
Running setup.py install for pyfst: finished with status 'error'
Complete output from command C:\pyth\pythonw.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\...\\AppData\\Local\\Temp\\pycharm-packaging\\pyfst\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\...\AppData\Local\Temp\pip-ykez_6hi-record\install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.5
creating build\lib.win-amd64-3.5\fst
copying fst\__init__.py -> build\lib.win-amd64-3.5\fst
warning: build_py: byte-compiling is disabled, skipping.
running build_ext
building 'fst._fst' extension
creating build\temp.win-amd64-3.5
creating build\temp.win-amd64-3.5\Release
creating build\temp.win-amd64-3.5\Release\fst
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\pyth\include -IC:\pyth\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" /EHsc /Tpfst/_fst.cpp /Fobuild\temp.win-amd64-3.5\Release\fst/_fst.obj -O2
_fst.cpp
fst/_fst.cpp(321): fatal error C1083: Cannot open include file: 'fst/symbol-table.h': No such file or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\amd64\\cl.exe' failed with exit status 2
----------------------------------------
Command "C:\pyth\pythonw.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\...\\AppData\\Local\\Temp\\pycharm-packaging\\pyfst\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\...\AppData\Local\Temp\pip-ykez_6hi-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\...\AppData\Local\Temp\pycharm-packaging\pyfst\
答案 2 :(得分:0)
首先使用getElementByID等获取元素,然后您可以通过 element.innerHTML
获取该行的实际内容
var tableRow = document.getElementById("tr3");
var content = tableRow.innerHTML;
alert(content);

table td {border: 1px solid black;}

<table>
<tr id="tr1"><td>1</td><td>2</td></tr>
<tr id="tr2"><td>3</td><td>4</td></tr>
<tr id="tr3"><td>5</td><td>6</td></tr>
</table>
&#13;
您可以使用onClick函数来获取值。