1

我有以下在循环中发生的脚本。我将如何做到这一点,所以我只需将它放在页面底部一次,而不是硬编码类?每个<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>
4

2 回答 2

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>
于 2013-07-17T22:26:40.423 回答
0

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>
于 2013-07-18T15:00:59.383 回答