select json_所有国家/地区的清单:HTML select和JSON

select json

select json

Ever needed a list of all the countries in the world to put as options in an HTML select? Well, here you go. And JSON too. You're welcome!

是否曾经需要将世界上所有国家的清单作为option到HTML select ? 好吧,你去。 还有JSON 。 别客气!

但是怎么... (But how...)

How was the list derived so you can update it once in a while? Glad you asked.

列表是如何得出的,以便您可以不时更新一次? 很高兴你问。

https://www.iso.org/obp/ui/#search/code/ is the page linked from W3 spec so it must be the real deal.

https://www.iso.org/obp/ui/#search/code/是从W3规范链接的页面,因此它必须是真实的交易。

Switch to 300 results per page so you can see all the countries.

每页切换到300个结果,以便您可以查看所有国家/地区。

Open console. Write magical ES.OMGWhatNext code:

打开控制台。 编写神奇的ES.OMGWhatNext代码:

Array.from($0.querySelectorAll('tr')).
  map(
    ({cells}) => 
      `<option value="${cells[2].innerText}">${cells[0].innerText}</option>`
  ).join('\n')

Wrap in select, done! Here's how it looks again.

包装select ,完成! 这又是它的样子

Similarly magical code give you JSON:

同样,神奇的代码为您提供JSON:

JSON.stringify(
  Array.from($0.querySelectorAll('tr')).map(
    ({cells}) => {return {[cells[2].innerText]: cells[0].innerText}}
  )
)

$ 0? ($0?)

One thing to note is the $0 which means the table of results on the ISO page. $0 is what you get when you inspect something in the console and you highlight the inspected DOM node. Instead of $0 you can currently use document.querySelectorAll('table')[2] or document.getElementsByClassName('v-table-table')[0] but who knows how soon the HTML of the page will change.

要注意的一件事是$ 0,它表示ISO页上的结果表。 当您在控制台中检查某些内容并突出显示所检查的DOM节点时,将获得$0 。 您现在可以使用document.querySelectorAll('table')[2]document.getElementsByClassName('v-table-table')[0]代替$ 0,但谁知道页面HTML会多久更改。

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/list-of-all-the-countries-html-select-and-json-too/

select json

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码中的问题在于,你在点击事件的处理函数中使用了 `return` 语句来返回 `jsonString`。然而,`addEventListener` 方法并不期望处理函数返回值,所以无法正确处理这个返回值。 如果你想在点击事件处理函数执行完毕后获取 `jsonString` 的值,可以考虑将其保存到外部变量或者调用另一个函数来处理。 下面是修改后的代码示例: ```javascript const start = document.getElementById("getstart").addEventListener("click", function () { let select_bu01 = 0; let select_bu02 = 0; let select_bu03 = 0; let select_bu04 = 0; let select_bu05 = 0; let select_y01 = 0; let select_y02 = 0; let select_y03 = 0; let select_y04 = 0; let select_y05 = 0; let select_y06 = 0; let select_y07 = 0; let select_y08 = 0; let select_y09 = 0; let select_y10 = 0; let select_h01 = 0; let select_h02 = 0; let ifMDM = 0; let tmp_data = { MDM: ifMDM, BU01: select_bu01, BU02: select_bu02, BU03: select_bu03, BU04: select_bu04, BU05: select_bu05, Year1: select_y01, Year2: select_y02, Year3: select_y03, Year4: select_y04, Year5: select_y05, Year6: select_y06, Year7: select_y07, Year8: select_y08, Year9: select_y09, Year10: select_y10 }; let jsonString = JSON.stringify(tmp_data); console.log(jsonString); // 在这里可以使用 jsonString 或者调用其他函数来处理它 }); ``` 在上述示例中,我移除了 `return` 语句,并将 `jsonString` 的值保留在了点击事件处理函数的内部。你可以在 `console.log` 后面的注释处使用 `jsonString` 或者调用其他函数来处理它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值