JavaScript,获取带有 id 名称的 td 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2310145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript, getting value of a td with id name
提问by Strawberry
Originally I was using input with an id and grabbing it's value with getElementById. Currently, I'm playing with <td>s. How can I grab the values of the <td>?
最初我使用带有 id 的输入并使用 getElementById 获取它的值。目前,我正在玩<td>s。如何获取 的值<td>?
Originally I used:
最初我使用:
<input id="hello">
document.getElementById("hello").value;
Now I want to get value of <td>, but I don't know how;
现在我想获得 的价值<td>,但我不知道如何;
<td id="test">Chicken</td>
<td>Cow</td>
Edit:What's the difference between textContent, innerHTML, and innerText?
编辑:是什么区别textContent,innerHTML和innerText?
回答by rahul
To get the text content
获取文本内容
document.getElementById ( "tdid" ).innerText
or
或者
document.getElementById ( "tdid" ).textContent
var tdElem = document.getElementById ( "tdid" );
var tdText = tdElem.innerText | tdElem.textContent;
If you can use jQuery then you can use
如果你可以使用jQuery,那么你可以使用
$("#tdid").text();
To get the HTML content
获取 HTML 内容
var tdElem = document.getElementById ( "tdid" );
var tdText = tdElem.innerHTML;
in jQuery
在 jQuery 中
$("#tdid").html();
回答by Pekka
use the
使用
innerHTML
property and access the tdusing getElementById()as always.
属性并像往常一样访问td使用getElementById()。
回答by anthares
Again with getElementById, but instead .value, use .innerText
再次使用 getElementById,而是使用 .value,使用 .innerText
<td id="test">Chicken</td>
document.getElementById('test').innerText; //the value of this will be 'Chicken'
回答by Sarfraz
For input you must have used valueto grab its text but for td, you should use innerHTMLto get its html/value. Example:
对于输入,您必须用来value获取其文本,但对于 td,您应该使用它innerHTML来获取其 html/value。例子:
alert(document.getElementById("td_id_here").innerHTML);
To get its text though, use:
要获取其文本,请使用:
alert(document.getElementById("td_id_here").innerText);
回答by Qwerty
If by 'td value' you mean text inside of td, then:
如果“td 值”是指 td 内的文本,则:
document.getElementById('td-id').innerHTML
回答by Select0r
Have you tried: getElementbyId('ID_OF_ID').innerHTML?
你有没有试过:getElementbyId('ID_OF_ID').innerHTML?
回答by Vipin
.innerTextdoesnt work in Firefox.
.innerText在 Firefox 中不起作用。
.innerHTMLworks in both the browsers.
.innerHTML适用于两种浏览器。
回答by Naveen Y
if i click table name its shown all fields about table. i did table field to show. but i need to know click function.
如果我单击表名,它会显示有关表的所有字段。我做了表字段来显示。但我需要知道点击功能。
My Code:
我的代码:
$sql = "SHOW tables from database_name where tables_in_databasename not like '%tablename' and tables_in_databasename not like '%tablename%'";
$result=mysqli_query($cons,$sql);
$count = 0;
$array = array();
while ($row = mysqli_fetch_assoc($result)) {
$count++;
$tbody_txt .= '<tr>';
foreach ($row as $key => $value) {
if($count == '1') {
$thead_txt .='<td>'.$key.'</td>';
}
$tbody_txt .='<td>'.$value.'</td>';
$array[$key][] = $value;
}
}?>

