Javascript innerHTML基本问题

时间:2017-11-22 11:34:54

标签: javascript html innerhtml

我正在尝试使用Javascript innerHTML属性将字符串插入HTML。 但它不起作用。我在第一次尝试使用WITH AllTablesAndFields AS ( SELECT DENSE_RANK() OVER(ORDER BY tbl.value('@name','nvarchar(max)')) AS TableInx ,tbl.value('@name','nvarchar(max)') AS TableName ,ROW_NUMBER() OVER(PARTITION BY tbl.value('@name','nvarchar(max)') ORDER BY (SELECT NULL)) AS FieldInx ,fld.value('@name','nvarchar(max)') AS FieldName ,fld.value('@type','nvarchar(max)') AS FieldType FROM @xml.nodes('/db/tables/table') AS A(tbl) OUTER APPLY tbl.nodes('fields/field') AS B(fld) ) ,AllTablesAndRows AS ( SELECT DENSE_RANK() OVER(ORDER BY tbl.value('@name','nvarchar(max)')) AS TableInx ,tbl.value('@name','nvarchar(max)') AS TableName ,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowInx ,rw.query('.') AS RowNode FROM @xml.nodes('/db/tables/table') AS A(tbl) OUTER APPLY tbl.nodes('rows/row') AS B(rw) ) ,AllValues AS ( SELECT rws.TableInx ,rws.TableName ,rws.RowInx ,ROW_NUMBER() OVER(PARTITION BY rws.RowInx ORDER BY (SELECT NULL)) AS ValueInx ,fld.value('text()[1]','nvarchar(max)') AS Val FROM AllTablesAndRows AS rws OUTER APPLY rws.RowNode.nodes('row/f') AS A(fld) ) SELECT flds.TableName ,flds.FieldName ,flds.FieldType ,vals.Val FROM AllValues AS vals FULL OUTER JOIN AllTablesAndFields AS flds ON vals.TableInx=flds.TableInx AND vals.ValueInx=flds.FieldInx; 函数时,我在前面添加了+-----------+-------------+-----------+----------------------------------------+ | TableName | FieldName | FieldType | Val | +-----------+-------------+-----------+----------------------------------------+ | Events | UniqueID | Integer | 3086647 | +-----------+-------------+-----------+----------------------------------------+ | Events | ID | AutoInc | 18091842 | +-----------+-------------+-----------+----------------------------------------+ | Events | Date | Date | 2017-11-14 | +-----------+-------------+-----------+----------------------------------------+ | Events | Time | Time | 21:32:10 | +-----------+-------------+-----------+----------------------------------------+ | Events | Code | Integer | 1 | +-----------+-------------+-----------+----------------------------------------+ | Events | UserID | Integer | 202 | +-----------+-------------+-----------+----------------------------------------+ | Events | UserGUID | String | {ED53D55D-4B99-41F3-95BD-6945A989155C} | +-----------+-------------+-----------+----------------------------------------+ | Events | ReaderID | Integer | 803 | +-----------+-------------+-----------+----------------------------------------+ | Events | Type | Integer | 32 | +-----------+-------------+-----------+----------------------------------------+ | Events | Modified | Boolean | null | +-----------+-------------+-----------+----------------------------------------+ | Events | ModifReason | String | null | +-----------+-------------+-----------+----------------------------------------+ ,因为它不起作用。

showtime()

2 个答案:

答案 0 :(得分:2)

而不是.value使用.innerHTML,而是尝试在html中插入文字。

.value用于输入类型,不用于html标签,也不用于在html标签内插入文本。



window.onload = function Hello() {
  var timeArea = document.getElementById("time");
  var text = "Hello";
  timeArea.innerHTML = text;
};

<div id="time">
   What time is it?
</div>
&#13;
&#13;
&#13;

同时删除hello()之后调用的window.onload函数。 window.onload将自动执行hello函数。

答案 1 :(得分:0)

这是因为Div元素没有value属性。 如果要插入一些文本,则需要将代码更改为:

<script>
    window.onload = function Hello() {
        var timeArea = document.getElementById("test");
        var text = "Hello";
        document.getElementById("test");
        timeArea.textContent = text;
        };
</script>
    <div id="time">
       <p id="test">

       </p>
    </div>

我创建了一个P元素,因此您可以随意修改其文本,并将其打印在p中。您可以使用任何其他元素作为H1,H2等。

textContent从该元素中选择文本并使其等于变量text打印其值。

我知道这已经得到了回答,但这只是另一种方式。