不必每次循环都重复脚本

时间:2013-07-17 22:17:49

标签: javascript php jquery html

我在循环中发生了以下脚本。我如何制作它所以我只需要将它放在页面的底部,而不是硬编码的类?每个<li>的脚本都会出现,这看起来非常笨重。

这是脚本

  <script>
    $(".<?php echo strtolower(get_field('code'));?>_btn").click(function(){
        $("#<?php echo strtolower(get_field('code'));?>").on('show', function () {

            $('iframe.fohl-mobile').attr("src","http://www.psfk.com/home#<?php echo get_field('code');?>");

        });
    });
  </script>

这是在循环中重复的整篇文章

 <li>
    <a data-toggle="modal" role="button" class="<?php echo strtolower(get_field('code'));?>_btn" href="#<?php echo strtolower(get_field('code'));?>">&nbsp;</a>

    <img class="lazy" alt="<?php the_title();?>" src="<?php echo site_url('wp-content/fohl/holder.png');?>" data-original="<?php echo site_url('/wp-content/fohl/products/').strtolower(get_field('code'));?>.png" />
    <noscript><img src="<?php echo site_url('/wp-content/fohl/products/').strtolower(get_field('code'));?>.png” alt="<?php the_title();?>"/></noscript>

    <div class="title"><?php the_title();?>

    <div id="<?php echo strtolower(get_field('code'));?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <iframe class="fohl-mobile" height="500" width="330" frameborder="0"  src=""></iframe>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
      </div>
    </div>
    <script>
        $(".<?php echo strtolower(get_field('code'));?>_btn").click(function(){
            $("#<?php echo strtolower(get_field('code'));?>").on('show', function () {

                $('iframe.fohl-mobile').attr("src","<?php echo site_url('/home#'.get_field('code'));?>");

            });
        });
    </script>
 </li>

2 个答案:

答案 0 :(得分:1)

也许这对你有用吗?它没有经过测试,但在概念上似乎没问题。我会测试,但你没有发布JSFiddle。基本上,对于循环的每次迭代,您的主触发器可以是通用类而不是不同的类,它引用其父级以获取更具体的内部内容。

HTML

<a data-toggle="modal" role="button" class="class_for_any_btn" href="#<?php echo strtolower(get_field('code'));?>">&nbsp;</a>

JS

<script>
        $(".class_for_any_btn").click(function(){
            $(this).parent().on('show',"#<?php echo strtolower(get_field('code'));?>", function () {

                $('iframe.fohl-mobile').attr("src","<?php echo site_url('/home#'.get_field('code'));?>");

            });
        });
</script>

答案 1 :(得分:0)

知道了。我在里面添加了一个id并从那里开始...感谢大家的帮助。

 <ul class="products">  
        <?php foreach($products as $post) : setup_postdata($post);?>  
        <li>
            <a data-toggle="modal" role="button" class="this_btn" href="#<?php echo get_field('code');?>">&nbsp;</a>

            <img class="lazy" alt="<?php the_title();?>" src="<?php echo site_url('/wp-content/fohl/holder.png');?>" data-original="<?php echo site_url('/wp-content/fohl/products/').strtolower(get_field('code'));?>.png" />
            <noscript><img src="<?php echo site_url('/wp-content/fohl/products/').strtolower(get_field('code'));?>.png" alt="<?php the_title();?>"/></noscript>

            <p class="title"><?php the_title();?></p>

            <div id="<?php echo get_field('code');?>" class="modal hide fade" class="goat" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <iframe class="fohl-mobile" height="500" width="330" frameborder="0"  src=""></iframe>
              <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
              </div>
            </div>
        </li>
    <?php endforeach;?>
    <script>
        $(".this_btn").click(function(){
            var id = $(this).parent().find('div').attr('id');

            $('.modal').on('show', function() {     
                $('.modal iframe.fohl-mobile').attr("src","http://www.website.com/home#"+id);
            });
        });
    </script>
 </ul>