Creating unique id with jquery to use in bxslider
I guess this is pretty simple but I have no experience with jquery whatsoever. I'm using bxslider in every post I post in my wordpress theme and need the jquery to point to numerous unique id's, not only to work1, but work2, work3 and so on.
The markup is this:
<ul id="work1">
<li></li>
<li></li>
</ul>
<ul id="work2">
<li></li>
<li></li>
</ul>
...
And the code:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#work1').after('<div class="work-pager"></div>');
jQuery('#work1').bxSlider({
mode: 'horizontal',
开发者_运维问答 infiniteLoop: false,
speed: 500,
pause: 8000,
auto: false,
pager: true,
controls: false,
pagerSelector: '.work-pager'
});
});
</script>
function findNewId() {
var i = 0;
while(1) {
if($('#work'+i).size()==0) return 'work'+i;
i++;
}
}
$('ul').each(function() {
$(this).attr('id',findNewId());
});
This should do the trick. JSFIDDLE
精彩评论