Drupal jQuery在.resize()上运行脚本

Drupal jQuery在.resize()上运行脚本,jquery,drupal,resize,Jquery,Drupal,Resize,我有以下代码,它获取主体高度,并将其写入一个div,该div显示为表格单元格,因此内容可以垂直居中于调整大小上的任何屏幕大小。我已经测试过了,效果很好 $( window).resize(function() { bodyHeight = $('body').outerHeight(); console.log('bodyHeight =' + bodyHeight); $('.table-cell').css('height', bodyHeight +'px'); }

我有以下代码,它获取主体
高度
,并将其写入一个div,该div显示为
表格单元格
,因此内容可以垂直居中于
调整大小
上的任何屏幕大小。我已经测试过了,效果很好

$( window).resize(function() {
    bodyHeight = $('body').outerHeight();
    console.log('bodyHeight =' + bodyHeight);
    $('.table-cell').css('height', bodyHeight +'px');
});
现在,如果我尝试使用下面的代码将其放入Drupal中。它不起作用。之所以调用jQuery,是因为我可以在Drupal中运行脚本,但当我指定在
resize
上运行脚本时就不行了

jQuery(window).resize(function($) {
    bodyHeight = $('body').outerHeight();
    console.log('bodyHeight =' + bodyHeight);
    $('.table-cell').css('height', bodyHeight +'px');
});

resize
方法中的第一个参数是
event
对象,它不是
jQuery

jQuery(window).resize(function(e) {
    bodyHeight = $('body').outerHeight();
    console.log('bodyHeight =' + bodyHeight);
    $('.table-cell').height(bodyHeight);
});
您也可以只使用
.height()
方法而不是
.css()
方法

如果您担心冲突的
$
变量,请将其全部打包为:

jQuery(function($){
 // Your window resize here.
 // $ will reference jQuery within this scope
});

resize
方法中的第一个参数是
event
对象,它不是
jQuery

jQuery(window).resize(function(e) {
    bodyHeight = $('body').outerHeight();
    console.log('bodyHeight =' + bodyHeight);
    $('.table-cell').height(bodyHeight);
});
您也可以只使用
.height()
方法而不是
.css()
方法

如果您担心冲突的
$
变量,请将其全部打包为:

jQuery(function($){
 // Your window resize here.
 // $ will reference jQuery within this scope
});

你已经在文档中编写了代码。准备好了吗?你已经在文档中编写了代码。准备好了吗?谢谢,伙计,你的第二个建议,用jQuery(函数($)包装它,效果很好。我也会尝试.height()方法。它听起来比.css()更干净。谢谢你的第二个建议,用jQuery(函数($)包装它,效果很好。我会尝试.height()方法。它听起来比.css()更干净