Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

W3.JS 显示 HTML 数据


在 HTML 中显示数据

w3.displayObject(selector)

易于使用

只需在任何 HTML 元素中添加方括号 {{ }} 来预留数据空间

示例

<div id="id01">
{{firstName}} {{lastName}}
</div>

最后调用 w3.displayObject 在您的 HTML 中显示数据

示例

<script>
var myObject = {"firstName" : "John", "lastName" : "Doe"};
w3.displayObject("id01", myObject);
</script>

自己试试 »

第一个参数是 HTML 元素的 ID(id01)。
第二个参数是要显示的数据对象(myObject)。


显示较大的对象

为了展示 W3.JS 的强大功能,我们将显示一个更大的 JavaScript 对象(myObject)。

该对象是客户对象的数组,包含 CustomerName、City 和 Country 属性

myObject

var myObject = {"customers":[
{"CustomerName":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"},
{"CustomerName":"Around the Horn","City":"London","Country":"UK"},
{"CustomerName":"B's Beverages","City":"London","Country":"UK"},
{"CustomerName":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"},
{"CustomerName":"Bon app'","City":"Marseille","Country":"France"},
{"CustomerName":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"},
{"CustomerName":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"}
]};

填充下拉菜单

示例

<select id="id01">
  <option w3-repeat="customers">{{CustomerName}}</option>
</select>

<script>
w3.displayObject("id01", myObject);
</script>

自己试试 » 使用 CSS »


填充列表

示例

<ul id="id01">
  <li w3-repeat="customers">{{CustomerName}}</li>
</ul>

<script>
w3.displayObject("id01", myObject);
</script>

自己试试 » 使用 CSS »


将 w3.displayObject 与 w3.includeHTML 结合使用

在网页中包含 HTML 片段时,您必须确保依赖包含 HTML 的其他函数在 HTML 正确包含之前不执行。

最简单的“阻止”代码的方法是将其放在回调函数中。

可以将回调函数作为参数添加到 w3.includeHTML() 中

示例

<div w3-include-html="list.html"></div>

<script>
w3.includeHTML(myCallback);

function myCallback() {
  w3.displayObject("id01", myObject);
}
</script>

自己试试 » 使用 CSS »


填充复选框

示例

<table id="id01">
  <tr w3-repeat="customers">
    <td>{{CustomerName}}</td>
    <td><input type="checkbox" {{checked}}"></td>
  </tr>
</table>

<script>
w3.displayObject("id01", myObject);
</script> 

自己试试 » 使用 CSS »


填充类

示例

<table id="id01">
  <tr w3-repeat="customers" class="{{Color}}">
    <td>{{CustomerName}}</td>
  </tr>
</table>

<script>
w3.displayObject("id01", myObject);
</script>

自己试试 » 使用 CSS »


填充表格

示例

<table id="id01">
  <tr>
    <th>客户</th>
    <th>城市</th>
    <th>国家</th>
  </tr>
  <tr w3-repeat="customers">
  <td>{{CustomerName}}</td>
  <td>{{City}}</td>
  <td>{{Country}}</td>
  </tr>
</table>

<script>
w3.displayObject("id01", myObject);
</script>

自己试试 » 使用 CSS »


填充 <select> 元素

示例

<select id="id01">
  <option w3-repeat="x in cars">{{x}}</option>
</select>

<script>
w3.displayObject("id01", {"cars" : ["Volvo", "Ford", "BMW", "Mercedes"]});
</script>

自己试试 » 使用 CSS »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.