简体   繁体   English

使用不同的参数循环调用jQuery函数

[英]Call jQuery function in a loop with different parameters

I am trying to create multiple carousels in one page following this example. 示例之后,我试图在一页中创建多个轮播。
I am creating my carousels in a foreach loop, and I assign to each carousel the names c0, c1, c2, etc. (Each carousel is a <div> ) 我在foreach循环中创建轮播,并为每个轮播分配名称c0,c1,c2等(每个轮播都是<div>
Now, in order to run the script according to the example, I should run in on each carousel separately. 现在,为了根据示例运行脚本,我应该分别在每个轮播中运行。
For example: 例如:

<script type="text/javascript">
    $(document).ready(function() {

        $('#c0').jsCarousel({ onthumbnailclick: function(src) { alert(src); }, autoscroll: true, masked: false, itemstodisplay: 3, orientation: 'v' });
        $('#c1').jsCarousel({ onthumbnailclick: function(src) { alert(src); }, autoscroll: false, masked: false, itemstodisplay: 5, orientation: 'h' });
        $('#c2').jsCarousel({ onthumbnailclick: function(src) { alert(src); }, autoscroll: true, masked: true, itemstodisplay: 5, orientation: 'h' });

    });       

</script>

Since my carousels are created in a foreach loop, I cannot know how many of them I will have, so I tried to call the function in a for loop: 由于我的轮播是在foreach循环中创建的,因此我不知道将有多少个,因此我尝试在for循环中调用该函数:

    for (int i = 0; i < counter; i++)
    {
        string cNum = "#c" + i.ToString();%>
        <script type="text/javascript">
            $(document).ready(function() {
                $(cNum).jsCarousel({ onthumbnailclick: function(src) { alert(src); }, autoscroll: true });
            });
        </script>
   <%} %>

I checked, and the cNum values are okay, it gets the values #c0, #c1, etc. but it can't recognize it as an equivalent to '#c0' etc. that were there initially. 我检查了一下, cNum值还可以,它得到的值是#c0,#c1等,但是它不能识别出它与最初存在的'#c0'等价。

How can I insert dynamic carousel names into the function? 如何在函数中插入动态轮播名称?

Instead of doing that, just give each div a class. 不必这样做,只需给每个div一个类。 Like this: 像这样:

<div class="someClassThatIKnowIsACarousel">

Then you don't need a loop: 然后,您不需要循环:

$(".someClassThatIKnowIsACarousel").jsCarousel({ onthumbnailclick: function(src) { alert(src); }, autoscroll: true });

The problem in your code is that cNum inside your dynamically generated JavaScript section isn't interpreted as an ASP variable. 您的代码中的问题在于,动态生成的JavaScript部分中的cNum不会被解释为ASP变量。 You could fix that with something like $('<% cNum %>') (also note the JavaScript quotes, without get you would get $(#c0) , which is erroneous). 您可以使用$('<% cNum %>')类的方法来解决此问题(还要注意JavaScript引号,如果没有得到,则会得到$(#c0) ,这是错误的)。

However, your approach is wrong, please avoid the best you can mixing server/client code like that. 但是,您的方法是错误的,请尽量避免那样的服务器/客户端代码混合使用。

As aquinas already pointed out, the best solution is to add a class to the divs: 正如阿奎纳斯已经指出的那样,最好的解决方案是向div中添加一个类:

HTML: HTML:

<div class="carousel">

JavaScript: JavaScript:

$('div.carousel').jsCarousel({ ... });

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

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