如何向所有 Drupal 表单添加 onmouseover 事件?

发布于 2024-08-19 09:24:42 字数 77 浏览 8 评论 0原文

我想向我的 drupal 网站上所有表单的提交按钮添加一个 onmouseover 事件。

我该怎么做?

I want to add an onmouseover event to the submit button of all forms on my drupal site.

How can I do this?

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-08-26 09:24:42

使用 jQuery:

$(document).ready(function(){
    $('input[type="submit"]').hover(
      function(){ 
        // Do something with $(this)
      }
    );
});

编辑: 使用另一种可能的解决方案进行更新,因为您只需要更新背景颜色。

如果您不关心 IE6 支持,也可以使用 :hover用于提交按钮的 CSS 伪选择器,不需要任何 javascript。确保所有提交按钮都设置了一个类(在本例中我使用 'submit' 作为类名)。

.submit {
  background-color: #ddd;
}
.submit:hover {
  background-color: #f00;
}

With jQuery:

$(document).ready(function(){
    $('input[type="submit"]').hover(
      function(){ 
        // Do something with $(this)
      }
    );
});

EDIT: Updated with another possible solution since you just need to update the background color.

If you don't care about IE6 support, you can also use the :hover CSS pseudo selector for your submit buttons and don't need any javascript. Make sure all your submit buttons have a class set to them (I'm using 'submit' as the class name in this example).

.submit {
  background-color: #ddd;
}
.submit:hover {
  background-color: #f00;
}
枯寂 2024-08-26 09:24:42

我通过将以下代码添加到自定义模块成功地部分解决了我的问题:

function defprofile_form_alter(&$form, &$form_state, $form_id) {
    $form['submit']['#attributes'] = array('onMouseOver' => "this.style.backgroundColor='#cc0000'",'onMouseOut' => "this.style.backgroundColor='#000'"); }

但是,这仅适用于提交按钮,我希望它适用于所有按钮。我尝试将“提交”替换为“按钮”,但随后代码完全停止工作。

I have managed to partly resolve my problem by adding the following code to a custom module:

function defprofile_form_alter(&$form, &$form_state, $form_id) {
    $form['submit']['#attributes'] = array('onMouseOver' => "this.style.backgroundColor='#cc0000'",'onMouseOut' => "this.style.backgroundColor='#000'"); }

However, this only works for submit buttons, and I want it to work for all buttons. I've tried replacing 'submit' with 'button' but then the code stops working altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文