Drupal 7 Jquery Action需要每次都发生而不是一次

时间:2011-11-30 12:29:17

标签: jquery drupal

我有一个drupal 7站点,我正在加载一个主题级别的jquery脚本,在我的.info文件中添加它:

scripts[] = script.js

我希望它每次点击一个链接时移动一个视图ajax pager(链接更改对hotornot.com上的内容进行投票)。我在我的script.js中使用的确切代码在下面,到目前为止它正在工作,但它只适用于第一次点击而不是之后。如何在每次点击$("li.pager-next a").click();时发生此a.ratebutton

(function($) {
  $(document).ready(function() {        
     $("a.rate-button").click(function(){

    // $(this).hide();
    $("li.pager-next a").click();
      // window.location = $("li.pager-next a").attr('href');

    });
  });
}(jQuery));  

2 个答案:

答案 0 :(得分:3)

您应该使用Drupal's JavaScript behavior system和jQuery Once而不是文档就绪。其他向页面添加内容的行为良好的代码应该使用Drupal.attachBehaviors(addedElement)附加所有已定义的行为。

(function($) {
  Drupal.behaviors.myFunkyTheme = {   
    'attach': function(context, settings) {
      $("a.rate-button", context).once('funky-theme').click(function(){
        // $(this).hide();
         $("li.pager-next a").click();
         // window.location = $("li.pager-next a").attr('href');
      });
    }
  }
}(jQuery));

答案 1 :(得分:0)

我不确定这是不是你的情况,但如果链接是由ajax重建的,你必须使用live()(如果你在1.7之前使用jQuery)

 $("a.rate-button").live('click', function(){

// $(this).hide();
   $("li.pager-next a").click();
  // window.location = $("li.pager-next a").attr('href');

   });

或on()jQuery 1.7及之后的

$("body").on("click", "a.rate-button", function(){
   $("li.pager-next a").click();
});
相关问题