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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改