简体   繁体   English

在Drupal的分页页面(第一页除外)上未加载自定义Javascript

[英]Custom Javascript Not Loading On Paginated Pages (Except First Page) In Drupal

I am running the following jQuery that affects elements on a page view. 我正在运行以下jQuery,这会影响页面视图上的元素。

$(document).ready(function($){                    
    $(".views-field-field-video").click(function() {
        $(this).parent().find("a.cboxElement").click();
    }); 
});

The code works perfectly, but only on the first page. 该代码运行良好,但仅在第一页上。 When I use the pager at the bottom and navigate to any other page, the script does not work. 当我在底部使用寻呼机并导航到任何其他页面时,该脚本不起作用。 Then, when I navigate back to the first page, the script also fails. 然后,当我导航回到第一页时,脚本也失败了。

If I reload the page however, it brings me back to the first page and the script works again. 但是,如果我重新加载页面,它将带我回到第一页,脚本将再次起作用。

I am linking a .js file, and link in between the <head> </head> tags using <script type="text/javascript" src="http://source_to_file"></script> It is loading, I can see in the web developer tool. 我正在链接.js文件,并使用<script type="text/javascript" src="http://source_to_file"></script>链接在<head> </head>标记之间,正在加载,我可以在Web开发人员工具中看到。

EDIT: The classes I am selecting in the script remains the same on all pages. 编辑:我在脚本中选择的类在所有页面上均相同。

It sounds like you are only adding click events when the page is first loaded. 听起来您只是在首次加载页面时才添加点击事件。 If you are then dynamically adding HTML (with pagination) then you need to re-add the click events to those new objects. 如果随后要动态添加HTML(带有分页),则需要将click事件重新添加到这些新对象。

Sounds like you just need to reatache the event. 听起来您只需要重新整理活动即可。 Drupal gives you a great way to do that right in your javascript: Drupal为您提供了一种在javascript中正确执行此操作的好方法:

Drupal.attachBehaviors('.behavior_class');

This will cause a lot of headache if you don't have context passed to your behaviors though. 如果您没有将上下文传递给您的行为,这将引起很多头痛。

Make sure your code is wrapped in, for drupal 6: 对于drupal 6,请确保将代码包装在其中:

Drupal.behaviors.clikfocus_map = function (context) {

for drupal 7: 对于drupal 7:

(function ($) {
  Drupal.behaviors.ajax_example = {
    attach:function (context) {
    }
  }
})(jQuery);

and your selectors should all have 您的选择器都应该有

$('.myselector', context');

Turns out I needed to just turn off AJAX on the view. 事实证明,我只需要在视图上关闭AJAX。 Sorry to over complicate things, but thank you for all your help! 抱歉使事情复杂化了,但是谢谢您的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM