dongqijuan3786 2012-02-25 00:22
浏览 48
已采纳

使用JQuery和Wildcards

Question
Is there a way to split up each class based upon a prefix using JQuery each and Wildcards?

EDIT: In my case I want to split the elements by rel instead of class.

Background
Right now I am just generating a separate code block for each fancybox group which is a really big hack, but it works. I would like to pull this code out of the php class and put it on the html view, and only have it written once versus having a new block for each example_group. I am sure that there is a way, but for now I am stuck with my temporary hack.

Current Code


        (function($) {//use jquery each
            $("a[rel=example_group$group]").fancybox({
                'transitionIn'      : 'none',
                'transitionOut'     : 'none',
                'titlePosition'     : 'over',
                'cyclic'            : true,
                'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
                    return 'Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? '   ' + title : '') + '';
                }
            });
        })(jQuery);

SOLUTION


        (function($) {
            $("a[rel^=example_group").fancybox({
                'transitionIn'      : 'none',
                'transitionOut'     : 'none',
                'titlePosition'     : 'over',
                'cyclic'            : true,
                'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
                    return 'Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? '   ' + title : '') + '';
                }
            });
        })(jQuery);
  • 写回答

2条回答 默认 最新

  • dongzhong8691 2012-02-25 00:27
    关注
    $('a[rel^="example_group"]');
    

    This will target any anchor whose rel value begins with "example_group". That means, it will target all of the following:

    <a ... rel="example_group1">Group 1</a>
    <a ... rel="example_group2">Group 2</a>
    <a ... rel="example_group_ponies">Ponies!</a>
    

    For more information, check out the Attribute Starts With Selector.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM