我正在使用element并希望它自动选择第一个匹配的元素。
我的想法是在单击时创建一个自定义的KeyboardEvent,然后在用户键入某些内容时(首先是在单击时)分派它,因此每次按下键时,都会以编程方式触发。
Direct output
{"rates":{"CAD":1.6409680499,"HKD":9.6691560447,"ISK":152.3948565332,"PHP":63.800325678,"DKK":8.3851984951,"HUF":373.6986916727,"CZK":28.9078555786,"GBP":1.0,"RON":5.3321354371,"SEK":12.1404907631,"IDR":17442.1809197597,"INR":87.4181593576,"BRL":5.0228536133,"RUB":79.8944353978,"HRK":8.3345499467,"JPY":131.652535235,"THB":37.5506766242,"CHF":1.2255601101,"EUR":1.1230276826,"MYR":5.16064911,"BGN":2.1964175417,"TRY":7.0194845303,"CNY":8.8154304004,"NOK":11.220731091,"NZD":1.9484530294,"ZAR":18.6923465663,"USD":1.2329720928,"MXN":24.1548655174,"SGD":1.7001516087,"AUD":1.824583076,"ILS":4.2960300971,"KRW":1473.7716884721,"PLN":4.8565332135},"base":"GBP","date":"2019-10-04"}
First JSON Parse attempt
rates - {"CAD"=>1.6409680499, "HKD"=>9.6691560447, "ISK"=>152.3948565332, "PHP"=>63.800325678, "DKK"=>8.3851984951, "HUF"=>373.6986916727, "CZK"=>28.9078555786, "GBP"=>1.0, "RON"=>5.3321354371, "SEK"=>12.1404907631, "IDR"=>17442.1809197597, "INR"=>87.4181593576, "BRL"=>5.0228536133, "RUB"=>79.8944353978, "HRK"=>8.3345499467, "JPY"=>131.652535235, "THB"=>37.5506766242, "CHF"=>1.2255601101, "EUR"=>1.1230276826, "MYR"=>5.16064911, "BGN"=>2.1964175417, "TRY"=>7.0194845303, "CNY"=>8.8154304004, "NOK"=>11.220731091, "NZD"=>1.9484530294, "ZAR"=>18.6923465663, "USD"=>1.2329720928, "MXN"=>24.1548655174, "SGD"=>1.7001516087, "AUD"=>1.824583076, "ILS"=>4.2960300971, "KRW"=>1473.7716884721, "PLN"=>4.8565332135}
base - GBP
date - 2019-10-04
JSON Parse output with nested each statements
CAD - 1.6409680499
HKD - 9.6691560447
ISK - 152.3948565332
PHP - 63.800325678
DKK - 8.3851984951
HUF - 373.6986916727
CZK - 28.9078555786
GBP - 1.0
RON - 5.3321354371
SEK - 12.1404907631
IDR - 17442.1809197597
INR - 87.4181593576
BRL - 5.0228536133
RUB - 79.8944353978
HRK - 8.3345499467
JPY - 131.652535235
THB - 37.5506766242
CHF - 1.2255601101
EUR - 1.1230276826
MYR - 5.16064911
BGN - 2.1964175417
TRY - 7.0194845303
CNY - 8.8154304004
NOK - 11.220731091
NZD - 1.9484530294
ZAR - 18.6923465663
USD - 1.2329720928
MXN - 24.1548655174
SGD - 1.7001516087
AUD - 1.824583076
ILS - 4.2960300971
KRW - 1473.7716884721
PLN - 4.8565332135
Traceback (most recent call last):
2: from ruby-api.rb:21:in `<main>'
1: from ruby-api.rb:21:in `each'
ruby-api.rb:23:in `block in <main>': undefined method `each' for "GBP":String (NoMethodError)
我希望执行的实际操作与按“向下箭头”键相同,但是此“模仿”事件不会执行任何操作。如果添加<!DOCTYPE html>
<html>
<body>
<form action="#">
<input list="country" id="list">
<datalist id="country">
<option value="Armenia">
<option value="Argentina">
</datalist>
</form>
<script>
list.addEventListener('click', function(event){
let key = new KeyboardEvent('keydown', {code: "ArrowDown", keyCode: 40})
list.dispatchEvent(key)
})
</script>
</body>
</html>
来监听addEventListener
事件,则永远不会触发该事件。