简体   繁体   English

如何在单击按钮时显示特定ACF行的内容

[英]How to display the content of a specific ACF row on button click

Thanks in advance for any help or input. 在此先感谢您的帮助或输入。

I have this code getting all the rows in an ACF Repeater and displaying them as square blocks. 我有这段代码在ACF转发器中获取所有行并将它们显示为正方形块。

<div class="class_wrapper"> 
<?php if( have_rows('classes') ): ?>

<div class="class_blocks">  
  <?php while( have_rows('classes') ) : the_row(); ?>
      <div class="class-callout-block" style="background:url(<?php the_sub_field('class_image'); ?>);">
      <h2><?php the_sub_field('title'); ?></h2>
        </div>

        <?php

    endwhile;

endif;

?>

</div>

What I am trying to achieve is when a block is clicked, the div below populates with the same content as the block clicked. 我要实现的是单击一个块时,下面的div填充的内容与单击的块相同。 ie. 即。 it calls the same repeater row. 它调用同一中继器行。

If anyone can point me in the right direction it would be greatly appreciated. 如果有人能指出我正确的方向,将不胜感激。

You want to copy the data of class-callout-block to another div. 您想要将class-callout-block的数据复制到另一个div。 By the loop I'm guessing there are many blocks like that and one output block. 通过循环,我猜有很多类似的块和一个输出块。 I'm not sure about the exact requirement, but I'll show you how to copy a div content & put it in another. 我不确定确切的要求,但我将向您展示如何复制div内容并将其放在另一个内容中。

Check the below code on how to do it and implement it yourself. 检查下面的代码,了解如何执行和自己实现。 The below code uses simple JavaScript functions and subscribing to an event listener. 下面的代码使用简单的JavaScript函数并订阅事件监听器。

 //This is for first example. I assign a function click event document.getElementById("copy-block").addEventListener("click", function(e) { document.getElementById("paste-block").innerHTML = e.target.innerHTML; //this replaces the content. }); //This is for second example. I assign a function click event for each element of the copy-block-item. same as first one, but in loop. var lis = Array.from(document.querySelectorAll(".copy-block-item")); for (var i = 0; i < lis.length; i++) { lis[i].addEventListener("click", function(e) { document.getElementById("paste-block-2").innerHTML = e.target.innerHTML; //this replaces the content. }); } 
 .box { width: 100px; height: 100px; border: 1px solid grey; float: left; } .paste{ border: 1px solid blue; } 
 <div class="box" id="copy-block">This contents needs to be copied to different div</div> <div class="box paste" id="paste-block"></div> <br/> <br/> <br/> <br/> <br/> <p> This is if you have multiple boxes</p> <div class="box copy-block-item">This contents needs to be copied to different div2</div> <div class="box copy-block-item">This contents needs to be copied to different div2 2</div> <div class="box copy-block-item">This contents needs to be copied to different div2 3</div> <div class="box paste" id="paste-block-2"></div> 

Please note: Array.from() is not supported in IE. 请注意:IE中不支持Array.from()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM