admin管理员组文章数量:1332865
I am having a problem with getElementsByTagName. I am trying to pull information from an xml file I received from the server using ajax. In IE it is works fine, but when I try to run it on Chrome or FireFox I get an "undefined".
Javascript:
function parseMessage()
{
doc = request.responseXML;
sum = "";
for (var i=0; i< doc.getElementsByTagName('coupon').length; i=i+1)
{
var lat2 = doc.documentElement.getElementsByTagName('latitude').item(i).text;
var longi2 = doc.documentElement.getElementsByTagName('longitude').item(i).text;
var latlng = new google.maps.LatLng( lat2 , longi2 );
var product = doc.documentElement.getElementsByTagName('productname').item(i).text;
// marker[i] = createMarker(map2, product, latlng, description);
sum = sum + description+ " "+ product + lat2 + longi2;
}
document.write(sum);
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
-<coupons>
-<coupon id="1">
<productname>Bigmac</productname>
<panyname>Macdonalds</panyname>
<latitude>32.015954</latitude>
<longitude>34.755228</longitude>
</coupon>
-<coupon id="2">
<productname>Crocs</productname>
<panyname>Crocs</panyname>
<latitude>32.079375</latitude>
<longitude>34.769325</longitude>
</coupon>
-<coupon id="3">
<productname>Nike Shoks</productname>
<panyname>NIKE</panyname>
<latitude>32.048825</latitude>
<longitude>34.785461</longitude>
</coupon>
-<coupon id="4">
.....
I am having a problem with getElementsByTagName. I am trying to pull information from an xml file I received from the server using ajax. In IE it is works fine, but when I try to run it on Chrome or FireFox I get an "undefined".
Javascript:
function parseMessage()
{
doc = request.responseXML;
sum = "";
for (var i=0; i< doc.getElementsByTagName('coupon').length; i=i+1)
{
var lat2 = doc.documentElement.getElementsByTagName('latitude').item(i).text;
var longi2 = doc.documentElement.getElementsByTagName('longitude').item(i).text;
var latlng = new google.maps.LatLng( lat2 , longi2 );
var product = doc.documentElement.getElementsByTagName('productname').item(i).text;
// marker[i] = createMarker(map2, product, latlng, description);
sum = sum + description+ " "+ product + lat2 + longi2;
}
document.write(sum);
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
-<coupons>
-<coupon id="1">
<productname>Bigmac</productname>
<panyname>Macdonalds</panyname>
<latitude>32.015954</latitude>
<longitude>34.755228</longitude>
</coupon>
-<coupon id="2">
<productname>Crocs</productname>
<panyname>Crocs</panyname>
<latitude>32.079375</latitude>
<longitude>34.769325</longitude>
</coupon>
-<coupon id="3">
<productname>Nike Shoks</productname>
<panyname>NIKE</panyname>
<latitude>32.048825</latitude>
<longitude>34.785461</longitude>
</coupon>
-<coupon id="4">
.....
Share
edited Jul 29, 2012 at 18:30
Josh Mein
28.7k15 gold badges78 silver badges88 bronze badges
asked Jul 29, 2012 at 18:25
Emil AdzEmil Adz
41.1k38 gold badges144 silver badges190 bronze badges
2
-
Given the posted XML, what are you trying to do? What is expected to happen from the use of
item(i)
? – David Thomas Commented Jul 29, 2012 at 18:36 - I am trying to get the information of the xml document and to create a marker from this information on google map for every coupon element in the file. – Emil Adz Commented Jul 29, 2012 at 18:51
1 Answer
Reset to default 6The first problem is that getElementsByTagName()
returns a list of elements; which can't be operated on as a group, so you'd have to select a particular node on which to work:
getElementsByTagName('latitude')[0]; // for the first element.
The second problem is that:
getElementsByTagName('latitude')[0].item(i);
Should be used to access a property, or attribute, of a particular element, and item(i)
is neither, though it may be set/used somewhere that we're not seeing.
If you're trying to access one of the element's children (and in your posted code the latitude
elements don't have any children), you could use:
getElementsByTagName('latitude')[0].childNodes[i];
And text()
doesn't return, or do, anything in plain JavaScript. If you're using the jQuery text()
method, then that only works on jQuery objects, if you're trying to access the text content of the current nodes, then:
getElementsByTagName('latitude')[0].textContent; // for Firefox, Chrome...
Or:
getElementsByTagName('latitude')[0].innerText; // for IE
本文标签: javascriptgetElementsByTagName 39undefined39 in Firefox and ChromeStack Overflow
版权声明:本文标题:javascript - getElementsByTagName 'undefined' in Firefox and Chrome? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742286726a2447060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论