javascript - 如何在javascript中为没有值的标签选择属性

标签 javascript html css

我有一个输入标签,我可以更改其属性,该属性需要一些值,例如

<input type="text">

<script>
    var inputEls = document.querySelector("input");
    inputEls.placeholder = "Enter Name";
</script>

但是我如何更改不采用自动对焦属性的放置属性。

inputEls.autofocus ?? 

最佳答案

autofocus 属性是一个 bool 属性,因此您将其设置为 truefalse:

inputEls.autofocus = true;

并非所有类似的属性都是 bool 值。例如,autocomplete 属性具有一个字符串值,可以是 "off""on"

您可以查找类似这样的内容 at the Mozilla Developer Network Wiki或者在庞大的 HTML5 规范中。 (编辑 - 我去寻找 W3C 规范的良好链接,但我找不到其中记录的 autofocus。)

请注意,设置 autofocus 标志至少在 Firefox 中似乎没有任何作用。我试过了

<!DOCTYPE html>
<html>
  <body>
    <input id=x>
    <script>
      document.getElementById("x").autofocus=true;
    </script>
  </body>
</html>

在一个小测试文件中,它在 Firefox 中不执行任何操作,但在 Chrome 中可以工作。同样,在 HTML 中使用“autofocus”属性进行相同的测试并将其设置为 false 的 JavaScript 也不会将其关闭。

在 Chrome 或 Firefox 中,在页面完全加载后设置标志似乎没有任何效果。

关于javascript - 如何在javascript中为没有值的标签选择属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28943090/

相关文章:

html - Bootstrap - 导航栏不会在移动设备上关闭

javascript - 如何阻止 <a> 标签跳转到 href 属性指定的目标?

css - 有没有办法设置 Google Chrome 默认 PDF 查看器的样式

html - 缩小选择的宽度,多个框

jQuery Mobile - 如何为 slider 设置透明背景?

javascript - 无法在带有 Nightwatch 的 Chai.js 中使用期望

javascript - Vue JS 在 console.log 上返回 [Object object] 并在 JSON.parse 上返回未定义

javascript - 对象构造函数和 TypeError 中的递归函数

javascript - window.frames[0].document.onkeydown 在 Firefox 中不起作用

javascript - 调用 webkitEnterFullscreen() 时出现 DOM 异常 11