Jquery为函数添加唯一标识符

发布于 2024-10-09 02:26:35 字数 724 浏览 1 评论 0原文

我正在使用 jQuery 循环插件。但是我需要在页面上添加所述代码的多个实例。这将通过一个循环传递。这意味着我必须向该函数添加一个唯一标识符。总的来说,我对 JavaScript 确实不太熟悉。

以下是原始代码

jQuery.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
    jQuery(pager).find('li').removeClass('mini-activeLI') 
        .filter('li:eq('+currSlideIndex+')').addClass('mini-activeLI'); 
}; 

这就是我正在尝试的。 其中 +unique+ 可以由 PHP rand() 生成:

jQuery.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex+unique+) { 
    jQuery(pager).find('li').removeClass('mini-activeLI') 
        .filter('li:eq('+currSlideIndex+unique++')').addClass('mini-activeLI'); 
}; 

任何帮助将不胜感激。

I'm using the jQuery cycle plugin. However I need to add multiple instances of said code on a page. This will be passed through a loop. This means I have to add a unique identifier to the function. I'm really not that comfortable with JavaScript in general.

Below is the original code.

jQuery.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
    jQuery(pager).find('li').removeClass('mini-activeLI') 
        .filter('li:eq('+currSlideIndex+')').addClass('mini-activeLI'); 
}; 

This is what I was trying.
Where +unique+ could be generated by PHP rand():

jQuery.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex+unique+) { 
    jQuery(pager).find('li').removeClass('mini-activeLI') 
        .filter('li:eq('+currSlideIndex+unique++')').addClass('mini-activeLI'); 
}; 

Any assistance would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2024-10-16 02:26:35

如果您对如何从 PHP 获取变量到 Javascript 感到困惑,您可以执行以下操作:

<?php 
    $unique = 123456;
    echo "<script>var unique = $unique;</script>";
?>

这将为您提供一个全局变量,您可以在其他脚本标记中使用它。

If you're confused about how to get a variable from PHP to Javascript, you can do something like:

<?php 
    $unique = 123456;
    echo "<script>var unique = $unique;</script>";
?>

Which will give you a global variable you can use from inside other script tags just fine.

吖咩 2024-10-16 02:26:35

试试这个:

function(pager, currSlideIndex<?php echo $id ?>) { 
    jQuery(pager).find('li').removeClass('mini-activeLI') 
        .filter('li:eq('+currSlideIndex<?php echo $id ?>+')').addClass('mini-activeLI'); 
}; 

编辑:将其作为变量传递有这样的:

<?php echo "<script type='text/javascript'>var _sliderIndexId = " + $id + ";</script>" ?>

然后:

function(pager, currSlideIndex) { 
        jQuery(pager).find('li').removeClass('mini-activeLI') 
            .filter('li:eq(' + (currSlideIndex + "") + (_sliderIndexId + "")) +')').addClass('mini-activeLI'); 
    };

不确定 PHP 语法,但这是一般想法:让 PHP 将 id 输出为全局 JS 变量,然后在函数中使用该变量。

Try this:

function(pager, currSlideIndex<?php echo $id ?>) { 
    jQuery(pager).find('li').removeClass('mini-activeLI') 
        .filter('li:eq('+currSlideIndex<?php echo $id ?>+')').addClass('mini-activeLI'); 
}; 

Edit: to pass it as variable have this:

<?php echo "<script type='text/javascript'>var _sliderIndexId = " + $id + ";</script>" ?>

And then:

function(pager, currSlideIndex) { 
        jQuery(pager).find('li').removeClass('mini-activeLI') 
            .filter('li:eq(' + (currSlideIndex + "") + (_sliderIndexId + "")) +')').addClass('mini-activeLI'); 
    };

Not sure about the PHP syntax but that's the general idea: have PHP output the id as global JS variable then use that variable in the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文