简体   繁体   English

放在.js单独文件上时jQuery函数不起作用

[英]jQuery function not working when placed on .js separate file

I have a php file with this jQuery function working properly (basically it identifies every '.limitCharacters' elements, calculates its width and height and makes some stuff on them): 我有一个与此jQuery函数正常工作的php文件(基本上,它标识每个'.limitCharacters'元素,计算其宽度和高度,并对它们进行一些处理):

$('.tableContent').find('.limitCharacters').each(function(){

    var percent = ((($(this).width()) / ( $(this).closest('div').width()))*100);
        if (percent > 95 || $(this).height() > 40){
         $(this).css('white-space','nowrap').css('color','red');
         $(this).parent().css('width', '85%').css('display','inline-block').css('overflow','hidden'); 
         $(this).parents().eq(1).css('height','36px');
         }      
  })

But when I try to move it to my separate .js file (which has already many functions inside being called from this php file, and works properfly), in order to call it from my php file whenever I need it, it does not work. 但是,当我尝试将其移动到单独的.js文件(已从该php文件调用了许多内部函数,并且可以正常运行)时,为了在需要时从我的php文件调用它,它无法正常工作。 This is how I've created it on my separate .js file: 这是我在单独的.js文件中创建它的方式:

function limitCharactersWidth(actualWidth, actualHeight, maxSpace, newSpace){
    if (actualWidth > maxSpace || actualHeight > 40){
         $(this).css('white-space','nowrap').css('color', 'red');
         $(this).parent().css('width', newSpace).css('display','inline-block').css('overflow','hidden'); 
         $(this).parents().eq(1).css('height','36px');}}

And this is the way I call it from my php file: 这就是我从php文件中调用它的方式:

$('.tableContent').find('.limitCharacters').each(function(){

    var actualWidth = ((($(this).width()) / ( $(this).closest('div').width()))*100);
    var height = $(this).height();
    limitCharactersWidth(actualWidth,height,'90','80');
  })

Theorically it is the same, just that in the second case I pass variables to my .js separate file and it should react the same. 从理论上讲,它是相同的,只是在第二种情况下,我将变量传递给了.js单独的文件,并且它应该做出相同的反应。 What am I missing? 我想念什么?

Your problem is $(this) , In your original code $(this) refers to the element .limitCharacters , As you have separated the function to separate JS file you have to pass the reference of $(this) to your function 您的问题是$(this) ,在原始代码中$(this)指向元素.limitCharacters ,因为您已经分离了函数以分离JS文件,所以必须将$(this)的引用传递给函数

Try this, Here I have added new parameter obj 试试这个,在这里我添加了新的参数obj

function limitCharactersWidth(obj, actualWidth, actualHeight, maxSpace, newSpace){
    if (actualWidth > maxSpace || actualHeight > 40){
        $(obj).css('white-space','nowrap').css('color', 'red');
        $(obj).parent().css('width', newSpace).css('display','inline-block').css('overflow','hidden'); 
        $(obj).parents().eq(1).css('height','36px');}}

Usage 用法

$('.tableContent').find('.limitCharacters').each(function(){
    var actualWidth = ((($(this).width()) / ( $(this).closest('div').width()))*100);
    var height = $(this).height();
    limitCharactersWidth(this, actualWidth,height,'90','80'); //Passed this
  })

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

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