doubu5035 2013-07-17 22:17 采纳率: 100%
浏览 40
已采纳

不必每次循环都重复脚本

I have the following script that happens within a loop. How would I make it so I only have to place it once at the bottom of a page and not have the classes hard coded in? The script occurs for each <li> which seems very clunky.

Here's the script

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

Here's the whole piece that repeats in the loop

 <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条回答 默认 最新

  • duandao1931 2013-07-18 15:00
    关注

    Got it. I added an id to the li and went from there...thanks for the help everyone.

     <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题