在[>]中显示来自JS表的html数据> 标题 我有一个JS表,该表由网站上用户在textareas中输入的数据生成(因此,当您第一次加载页面时,数据不存在:您必须在textarea中输入内容,然后生成该表)。现在,我试图从生成的表中获取该数据,并以(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> 但是当我单击时,我得到的是H2。“ document.getElementById('excel_table1')。getElementsByTagName('tr')[0] .cells [1]” 我有一个JS表,该表由网站上用户在textareas中输入的数据生成(因此,当您第一次加载页面时,数据不存在:您必须在textarea中输入一些内容,然后... ] > 在您的情况下,问题似乎是表达式周围的多余"以获取值。 如果值已经是一个字符串,则不需要用引号将其引起来,因为这样做会使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”(是的,它在另一个.innerHTML xD中创建了.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>

问题描述 投票:0回答:3

我有一个JS表,该表由网站上用户在textareas中输入的数据生成(因此,当您第一次加载页面时,数据不存在:您必须在textarea中输入内容,然后生成该表)。现在,我试图从生成的表中获取该数据,并以(h2 /)表示它。

javascript java html get getelementbyid
3个回答
1
投票

在您的情况下,问题似乎是表达式周围的多余"以获取值。


0
投票

确定,解决了!谢谢大家使我走上正轨:D

所以,什么对我有用:


0
投票

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

确保已为<h2>分配了ID。

© www.soinside.com 2019 - 2024. All rights reserved.