Jquery add Unique identifier to Function
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.
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.
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.
精彩评论