W3.JS Display
2020-11-05 17:02 更新
在 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>
var myObject = {"firstName" : "John", "lastName" : "Doe"};
w3.displayObject("id01", myObject);
</script>
尝试一下 »
第一个参数是要使用的HTML元素的id (id01)。
第二个参数是要显示的数据对象(myObject)。
显示较大的对象
来演示 W3 的强大功能。JS,我们将显示一个更大的 JavaScript 对象 (myObject)。
该对象是一个用户对象数组,具有 CustomerName、城市和国家属性:
我的对象
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"}
]};
{"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>
<script>w3.displayObject("id01", myObject);</script>
尝试一下 » 添加上CSS »
填充列表
实例
<ul id="id01"><li w3-repeat="customers">{{CustomerName}}</li></ul>
<script>w3.displayObject("id01", myObject);</script>
<script>w3.displayObject("id01", myObject);</script>
尝试一下 » 添加上CSS »
综合 w3.displayObject 和 w3.includeHTML
在 web 页面中包含 HTML 片段时,必须确保在正确包含 HTML 之前不会执行依赖于所包含 HTML 的其他函数。
“保留”代码的最简单方法是将其放在回调函数中。
可以在 w3.includeHTML()
中添加回调函数作为参数:
填充复选框
填充类
填充表格
填充<select>元素
以上内容是否对您有帮助:
更多建议: