仅使用javascript更改选择选项的顺序

时间:2017-11-05 23:02:43

标签: javascript html css fonts

当我点击按钮时,我试图颠倒字体选择选项的顺序。我在网上看到的一切都是使用jquery,有什么方法可以用js做到吗?

<button class="gray" type="button" onclick="sortFont();" > click </button>
<select id="fontMenu" >
   <option value="Arial">Arial</option>
   <option value="Times New Roman">Times New Roman</option>
</select>

1 个答案:

答案 0 :(得分:5)

这里我只是获取选项,放在一个数组中。相反。然后将它们添加回来。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict/>
</dict>
document.querySelector("button").onclick = () => {
  const fontMenu = document.querySelector("#fontMenu");
  const options = Array.from(fontMenu.querySelectorAll("option"));
  options.reverse();
  options.forEach((o) => fontMenu.appendChild(o));
}

相关问题