从XML文件中排除一组单词,并将它们存储在Javascript中的数组中
Disecting a group of words from an XML file and storing them in an array in Javascript
这里有这个XML文件。
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<word>
<threeletter>RIP</threeletter>
<threeletter>PIE</threeletter>
<fourletter>PIER</fourletter>
<fourletter>RIPE</fourletter>
<fiveletter>SPIRE</fiveletter>
<sixletter>SPIDER</sixletter>
</word>
<word>
<threeletter>SUE</threeletter>
<threeletter>USE</threeletter>
<fourletter>EMUS</fourletter>
<fourletter>MUSE</fourletter>
<fiveletter>SERUM</fiveletter>
<sixletter>RESUME</sixletter>
</word>
</xml>
然后我会加载它们,并在页面加载后将这些单词存储在一个名为word的数组中
$(document).ready(function() {
$.ajax
({
url: "dictionary.xml",
success: function( xml )
{
$(xml).find("word").each(function()
{
words.push($(this).text());
});
}
});
})
然后当我访问alert(word[0])
的每个内容时,它会向我显示这个结果
RIP
PIE
PIER
RIPE
SPIRE
SPIDER
所以我假设单词[0]是这样的,word[0] = "RIP PIE PIER RIPE SPIRE SPIDER "
但是当我做这个"
var x = word[0].split(" ");
alert(x[0]);
它没有给我"RIP"这个词知道为什么会发生这种事吗?我想去掉words[0]
中的所有单词(来自xml),然后拆分这些单词并将这些单词存储在一个数组中,但似乎不起作用——不知道为什么?
可能类似于此
$.ajax({
url: 'dictionary.xml',
async: false,
success: function(xml) {
$(xml).find("word").each(function(index) {
words[index] = [];
$(this).children().each(function() {
words[index].push($(this).text());
});
});
},
dataType: 'XML'
});
console.log(words[0]);
console.log(words[1]);
相关文章:
- Meteor会话变量未存储数组
- 在Javascript的Textbox中存储数组中的值
- 如何保留输入复选框值的存储数组
- 如何在 localStorage 中存储数组
- 如何在打包的 chrome 应用中本地存储数组
- 如何在 html5 的本地存储对象中存储数组
- 需要循环访问 json 数据的本地存储数组中存储的 json 对象
- 在 Javascript 中创建存储数组,并在每次单击时推送元素 ID
- 根据选中的复选框存储数组元素
- 如何存储数组javascript
- 存储数组在Chrome扩展
- 什么是最有效的方式来存储数组从PHP到JS/jQuery,这样它是可重用的
- Javascript存储数组值
- 存储数组到Cookie Jquery
- Ruby在遍历函数时尝试存储数组元素
- 如何从mongodb的形式存储数组
- 如何在html中打印存储在本地存储数组中的值
- 使用Firebase存储数组
- 获取字符串格式的MySQL存储数组并转换为JSON对象的数组
- 在html attr中存储数组值的最佳实践