Javascript jQuery函数放在.js单独文件中时不工作

Javascript jQuery函数放在.js单独文件中时不工作,javascript,jquery,Javascript,Jquery,我有一个php文件,它的jQuery函数工作正常(基本上它识别每个'.limitCharacters'元素,计算其宽度和高度,并在其上制作一些东西): 但是,当我试图将其移动到单独的.js文件中(该文件中已经有许多函数从这个php文件中调用,并且工作正常),以便在需要时从我的php文件中调用它时,它不起作用。以下是我在单独的.js文件中创建它的方式: function limitCharactersWidth(actualWidth, actualHeight, maxSpace, newSpa

我有一个php文件,它的jQuery函数工作正常(基本上它识别每个
'.limitCharacters'
元素,计算其宽度和高度,并在其上制作一些东西):

但是,当我试图将其移动到单独的.js文件中(该文件中已经有许多函数从这个php文件中调用,并且工作正常),以便在需要时从我的php文件中调用它时,它不起作用。以下是我在单独的.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');}}
这是我从php文件中调用它的方式:

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

    var actualWidth = ((($(this).width()) / ( $(this).closest('div').width()))*100);
    var height = $(this).height();
    limitCharactersWidth(actualWidth,height,'90','80');
  })
理论上是一样的,只是在第二种情况下,我将变量传递给.js单独的文件,它的反应应该是一样的。我遗漏了什么?

您的问题是
$(this)
,在您的原始代码中
$(this)
指的是元素
。limitCharacters
,当您将函数分隔为单独的JS文件时,您必须将
$(this)
的引用传递给您的函数

试试这个,我在这里添加了新参数
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');}}
用法

您的问题是
$(this)
,在原始代码中
$(this)
指的是元素
.limitCharacters
,因为您已将函数分隔为单独的JS文件,所以必须将
$(this)
的引用传递给您的函数

试试这个,我在这里添加了新参数
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');}}
用法

您的问题是
$(this)
,在原始代码中
$(this)
指的是元素
.limitCharacters
,因为您已将函数分隔为单独的JS文件,所以必须将
$(this)
的引用传递给您的函数

试试这个,我在这里添加了新参数
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');}}
用法

您的问题是
$(this)
,在原始代码中
$(this)
指的是元素
.limitCharacters
,因为您已将函数分隔为单独的JS文件,所以必须将
$(this)
的引用传递给您的函数

试试这个,我在这里添加了新参数
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');}}
用法


你说的消费是什么意思?我一开始以为是(这个),但是有很多其他的功能可以根据(这个)确定在哪里行动。请给我解释一下消费是什么意思来回答你。你说的消费是什么意思?我一开始以为是(这个),但是有很多其他的功能可以根据(这个)确定在哪里行动。请给我解释一下消费是什么意思来回答你。你说的消费是什么意思?我一开始以为是(这个),但是有很多其他的功能可以根据(这个)确定在哪里行动。请给我解释一下消费是什么意思来回答你。你说的消费是什么意思?我一开始以为是(这个),但是有很多其他的功能可以根据(这个)确定在哪里行动。请给我解释一下消费对你的回答意味着什么。我马上就要告诉你了。这很有效。非常感谢,我会在7分钟内打勾(系统在9分钟回复前不接受任何打勾)。谢谢你。@Biomehanika,很高兴能帮助Mate。我马上就要告诉你了。这非常有效。非常感谢,我会在7分钟内打勾(系统在9分钟回复前不接受任何打勾)。谢谢你。@Biomehanika,很高兴能帮助Mate。我马上就要告诉你了。这非常有效。非常感谢,我会在7分钟内打勾(系统在9分钟回复前不接受任何打勾)。谢谢你。@Biomehanika,很高兴能帮助Mate。我马上就要告诉你了。这非常有效。非常感谢,我会在7分钟内打勾(系统在9分钟回复前不接受任何打勾)。谢谢你。@Biomehanika,很高兴能帮助mate