-1

我在 javascript 中有一个包含数组的对象。我想知道如何使用类标签访问这些值?我的对象如下所示:

address_components: Array[8]
adr_address: "<span class="street-address">1808 N Clark St</span>, <span class="locality">Chicago</span>, <span class="region">IL</span> <span class="postal-code">60614</span>, <span class="country-name">USA</span>"

我想使用'postal-code'. 抱歉,如果这是一个重复的问题,无法找到解决方案,谢谢。

4

1 回答 1

1

Working FIDDLE Demo

考虑你有一个这样的字符串:

var adr_address = '<span class="street-address">1808 N Clark St</span>, <span class="locality">Chicago</span>, <span class="region">IL</span> <span class="postal-code">60614</span>, <span class="country-name">USA</span>';

动态创建一个元素,将innerHTML其设置为该字符串,然后获取您的postal-code元素:

var adr_address = '<span class="street-address">1808 N Clark St</span>, <span class="locality">Chicago</span>, <span class="region">IL</span> <span class="postal-code">60614</span>, <span class="country-name">USA</span>';
var elem = document.createElement('div');
elem.innerHTML = adr_address;
var postal = elem.getElementsByClassName('postal-code')[0].innerHTML;
alert(postal); // 60614
于 2013-05-17T16:34:51.207 回答