>我从XML获取数据。我需要检查数据,如果数据为空,我必须隐藏它。
如何检查?
<script>
downloadUrl("gxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var oid = markers[i].getAttribute("objectid");
var status = markers[i].getAttribute("status");
var title = markers[i].getAttribute("title");
var volume = markers[i].getAttribute("volume");
var kwh = markers[i].getAttribute("kwh");
var puser = markers[i].getAttribute("puser");
var ucomp = markers[i].getAttribute("ucomplate");
var udate = markers[i].getAttribute("udate");
var suser = markers[i].getAttribute("suser");
var scomp = markers[i].getAttribute("scomplate");
var sdate = markers[i].getAttribute("sdate");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "ObjectID:" + oid + " <br/> Title:" + title +" <br/>Status:" + status + " <br/>Volume:" + volume + " <br/> KWh:" + kwh + " <br/>User:" + puser + " <br/> User Date:" + udate + " <br/> User Complate :" + ucomp + " <br/>Super user:" + suser + " <br/> S .User Date:" + sdate + " <br/> S. User Complate :" + scomp + "<br/> <a href="edit.php">Add/Edit</a>";
</script>
// this data i get from xml. i`m need to chek this null or not null
您可以简单地对任何给定变量进行if (!myVariable)
检查。如果返回 true(也就是说,如果代码进入if
体,则数据为 null。我要做的是而不是这样做:
var oid = markers[i].getAttribute("objectid");
// ...
..."ObjectID:" + oid + "
在变量中包含您的标题,如下所示:
var oid = markers[i].getAttribute("objectid");
if (oid) {
oid = "ObjectID: " + oid;
}
这样,您就可以将oid
直接添加到字符串中。如果值为 null,则添加它将不起作用,因为变量将为空。如果有值,则将附加标题,并且执行+ oid
将同时附加标题和值。
if ( !foo ) {
// foo is not set, it is null.
}