在标题中<h2/>显示 JS 表中的 html 数据



我有一个JS表,它是由用户在我的网站上的文本区域中输入的数据生成的(所以当你第一次加载页面时,数据不在那里:你必须在文本区域输入一些东西,然后生成表(。现在,我尝试(从生成的表中(获取数据,并以(h2/(为例进行显示。

所以我制作了一个脚本来获取这些信息:

<script type="text/javascript">
function buttonTEST()
{
document.getElementById('excel_table1').getElementsByTagName('tr')[0].cells[1].innerHTML;
}
</script>

和使用该脚本的按钮:

<input id=GetTest type="button" onclick="buttonTEST()" value="TEST ME"/>

当我在控制台中输入"document.getElementById….etc"时,它会显示我想要的数据,所以我认为脚本很好。

但是,我如何使它以(h2(纯文本显示数据(字母数字(呢?

有人能帮忙吗?:(

编辑我试过这个:

<div>
<script type="text/javascript">function buttonTEST(){document.getElementById("yourID").innerHTML="document.getElementById('excel_table1').getElementsByTagName('tr'). 
[0].cells[1]";
}
</script>
<h2 id="yourID"></h2>
<input id="anotherID" type="button" onclick="buttonTEST()" value="TEST ME"/>
</div>

但当我点击时,我只得到"document.getElementById('excel_table1'(.getElementsByTagName('tr'([0].cells[1]">作为H2。

在您的例子中,问题似乎是表达式周围有额外的"来获取值。

如果一个值已经是一个字符串,则不需要用引号将其括起来,因为这样做会使javascript认为您想将该段代码随意写入h2输出

<div>
<script type="text/javascript">
function buttonTEST() {
document.getElementById("yourID").innerHTML = document.getElementById('excel_table1').getElementsByTagName('tr').
[0].cells[1].innerHTML;
}
</script>
<h2 id="yourID"></h2>
<input id="anotherID" type="button" onclick="buttonTEST()" value="TEST ME"/>
</div>

好的,解决了!感谢大家让我走上正轨:D

所以,对我有效的是:

<script type="text/javascript">
function buttonTEST()
{
document.getElementById('yourID').innerHTML=document.getElementById('excel_table1').getElementsByTagName('tr')[0].cells[1].innerHTML;
}
</script>
<h2 id="yourID"></h2>
<input id="anotherID" type="button" onclick="buttonTEST()" value="TEST ME"/>
</div>

@vhoyer是对的,我需要去掉多余的">,否则JS认为我想把那段代码随意写入h2输出。

另一个想法是,我需要在我所有的tr和单元格之后再次放上".innerHTML"(是的,它在另一个.innerHTMLxD中生成了.innerHTML(

希望这能帮助到别人!

干杯

您需要在html文件中创建一个<h2>元素,以使用javascript替换其值(在html文件中将为空(。

确保您已为<h2>分配了id。

然后,使用document.getElementById,在脚本中访问它并更改它。

下面是一个例子。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<h2 id="yourID"></h2>
<input id="anotherID" type="button" onclick="buttonTest()" value="TEST ME"/>
<script>
function buttonTest() {
//Grab the ID and change it
document.getElementById("yourID").innerHTML = "whatever you want it to display";
}
</script>
</body>
</html>

此外,请确保将html属性的值用单引号或双引号括起来。

编辑:

以下是您的用例示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<h2 id="yourID"></h2>
<input id="anotherID" type="button" onclick="buttonTest()" value="TEST ME" />
<p>Press me to find out the second cell in the second row</p>
<table id="table">
<tr>
<th>Color</th>
<th>Object</th>
</tr>
<tr>
<td>Red</td>
<td>Poppy</td>
</tr>
</table>
<script>
function buttonTest() {
//Here we grab the value of the second cell in the second row in a variable called value. You can change the row and cell index to access specific values.
var value = document.getElementById("table").rows[1].cells[1].innerHTML;
//Then we display it
document.getElementById("yourID").innerHTML = value;
}
</script>
</body>
</html>

最新更新