gpt4 book ai didi

javascript - 为什么这行脚本不起作用?

转载 作者:行者123 更新时间:2023-12-02 19:29:38 26 4
gpt4 key购买 nike

当调用数据字时,我想要显示和播放声音和图像。我尝试了很多不同的方法,但无法弄清楚。

这是一种方法...

for(i = 0; i < ul.children.length; ++i){

listOfWords[ul.children[i].getAttribute("data-word")] = ul.children[i].getAttribute("data-pic", "data audio");

}
console.log(listOfWords);

我也尝试过...

for(i = 0; i < ul.children.length; ++i){

listOfWords[ul.children[i].getAttribute("data-word")] = ul.children[i].getAttribute("data-pic");
listOfWords[ul.children[i].getAttribute("data-word")] = ul.children[i].getAttribute("data-audio");


}
console.log(listOfWords);

但没有这样的运气。

第二种方法执行底部但不执行顶部,当调用数据字时我需要数据音频和数据图片。

有人可以帮忙吗?

HTML...

<ul style="display:none;" id="wordlist">
<li data-word="mum" data-audio="http://www.wav-sounds.com/cartoon/daffyduck1.wav" data-pic="http://pixabay.com/static/uploads/photo/2012/04/13/00/06/head-31117_640.png"></li>
<li data-word="cat" data-audio="http://www.wav-sounds.com/cartoon/porkypig1.wav" data-pic="http://pixabay.com/static/uploads/photo/2012/05/03/23/13/cat-46676_640.png"></li>
<li data-word="dog" data-audio="http://www.wav-sounds.com/cartoon/porkypig1.wav" data-pic="http://pixabay.com/static/uploads/photo/2012/05/02/21/14/gray-46364_640.png"></li>
<li data-word="bug" data-audio="http://www.wav-sounds.com/cartoon/porkypig1.wav" data-pic="http://pixabay.com/static/uploads/photo/2012/04/16/12/17/black-35741_640.png"></li>
<li data-word="log" data-audio="http://www.wav-sounds.com/cartoon/daffyduck1.wav" data-pic="http://pixabay.com/static/uploads/photo/2012/04/13/11/18/fire-31929_640.png"></li>
<li data-word="dad" data-audio="http://www.wav-sounds.com/cartoon/daffyduck1.wav" data-pic="http://pixabay.com/static/uploads/photo/2012/04/13/00/05/old-31110_640.png"></li>

最佳答案

类似这样的事情:

for(i = 0; i < ul.children.length; ++i){
listOfWords[ul.children[i].getAttribute("data-word")] = {
"pic" : ul.children[i].getAttribute("data-pic"),
"audio" : ul.children[i].getAttribute("data-audio")
};
}

然后对于 listOfWords 对象中的给定项目,您可以执行以下操作:

var currentWord = "cat"; // set current word key somehow, then:
console.log(listOfWords[currentWord].pic);
console.log(listOfWords[currentWord].audio);

使用 jQuery:

var listOfWords = {};
$("#wordlist li").each(function() {
var $item = $(this);
listOfWords[ $item.attr("data-word") ] = {
pic : $item.attr("data-pic"),
audio : $item.attr("data-audio")
};
});

关于javascript - 为什么这行脚本不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11671219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com