【问题标题】:How to call mouse events in text field如何在文本字段中调用鼠标事件
【发布时间】:2012-01-03 17:42:56
【问题描述】:

我想在鼠标悬停时将焦点放在文本字段上。这个怎么做?现在我的代码看起来像这样

  <div class='label_input'>
    <%= f.label :username %>
    <%= f.text_field :username %>
    <div class='clearfix'></div>
  </div>

期望类似于 HTML 代码

<input type="text" onmouseover="this.focus()"></input>

【问题讨论】:

    标签: html ruby-on-rails actionview


    【解决方案1】:

    您可以将onmouseover 传递给text_field (docs)。

    【讨论】:

      【解决方案2】:

      有时我只是跳过erb并编写html

      你可以试试:

      <div class='label_input'>
          <%= f.label :username %>
          <input id="user_name" name="user[name]" size="30" type="text" onmouseover="this.focus()"></input>
          <div class='clearfix'></div>
        </div>
      

      我发现在某些情况下,rails 视图助手比 html 更麻烦。所以,用最简单的吧。

      当您使用 javascript 更新选择框时,这种方式非常有用。

      【讨论】:

        【解决方案3】:
        <div class='label_input'>
          <%= f.label :username %>
          <%= f.text_field :username, onmouseover: "this.focus()" %>
          <div class='clearfix'></div>
        </div>
        

        【讨论】:

          猜你喜欢
          • 2016-09-22
          • 2018-10-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多