douxian4323
2011-01-27 04:52
浏览 36
已采纳

向Drupal.behaviors添加函数

I'm developing a Drupal module. I need to set the focus to the first form element when a page is loaded. I read this can be done by using the jquery code -$('#edit-name').focus();.

How do I add this code to the Drupal.behaviors so that my code will be automatically called when the page is loaded?

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个Drupal模块。 我需要在加载页面时将焦点设置为第一个表单元素。 我读过这个可以通过使用jquery代码来完成 - $('#edit-name')。focus();

如何添加这个 代码到Drupal.behaviors,以便在加载页面时自动调用我的代码?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanbing6955 2011-01-27 10:14
    已采纳

    Here is a example on how to use behaviors: Drupal.behaviors.

    So all you need to do is put this in a .js file (or add it with inline): For Drupal6:

    Drupal.behaviors.focus = function (context) {
        $('#edit-name').focus();
    };
    

    For Drupal7:

    (function($) {
       Drupal.behaviors.focus = {
           attach:function(context,settings) {
               $('#edit-name').focus();
        }
       }
    })(jQuery);
    
    点赞 打赏 评论
  • dsztc99732 2011-01-27 04:57

    drupal_add_js("$('#edit-name').focus();", "inline"); in module. In theme it's depend where did you want this script, possible just adding in page-XXX.tpl.php direct code of script. In template.php in preprocessors you can use above code.

    点赞 打赏 评论

相关推荐 更多相似问题