JSON.parse获取了一个 json 对象,想每按一下按钮,获取json 中的一个数据

var wordsList= JSON.parse(sessionStorage.getItem("wordslist"));

假设有这样一个 json 对象,里面存了一些单词和对应的解释

{
    ‘hello’:'你好'
    ‘world’:'世界'
}

我想要按一下按钮,在界面上显示相应的单词,不按按钮的时候不显示


翻翻过去那场雪
浏览 952回答 1
1回答

慕哥6287543

<script>&nbsp; &nbsp; var wordsList= {&nbsp; &nbsp; &nbsp; &nbsp; 'hello':'你好',&nbsp; &nbsp; &nbsp; &nbsp; 'world':'世界'&nbsp; &nbsp; };&nbsp; &nbsp; var listWords = {};&nbsp; &nbsp; for(var key in wordsList){&nbsp; &nbsp; &nbsp; &nbsp; listWords[wordsList[key]]=key;&nbsp; &nbsp; }</script><input type="button" onclick="alert(listWords[this.value]);" value="你好">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript