简体   繁体   English

创建动态Javascript OnClick()

[英]Creating Dynamic Javascript OnClick()

I am using ACF repeater to create user profiles that have a "read more/read less" function that shows and hides text. 我正在使用ACF转发器来创建具有“阅读更多/阅读较少”功能的用户配置文件,该功能可以显示和隐藏文本。 The issue I'm facing is when I press the "read more" button for the 3rd profile down, it opens the first profile. 我面临的问题是,当我按下第三个配置文件的“更多”按钮时,它将打开第一个配置文件。 How do I make this functionality dynamic so it only opens/closes the profile being clicked? 如何使此功能动态化,使其仅打开/关闭被单击的配置文件?

<div class="container" style="margin-top: 60px;">
        <?php if(have_rows('profile')): ?>
        <?php while(have_rows('profile')): the_row(); ?>
            <div class="row">
                <div class="col-md-3">
                    <div class="people-img" style="background-image: url('<?php echo the_sub_field("profile_img"); ?>')"></div>
                        </div>
                        <div class="col-md-9">
                        <h4 class="profile-name">
                        <?php the_sub_field('name'); ?>
                        </h4>
                    <p class="profile-job-title semi-bold">
                                <?php the_sub_field('info'); ?>
                    </p>

                    <p><?php the_sub_field('profile_blurb'); ?><span id="dots">...</span><span id="more"><?php the_sub_field('profile_read_all'); ?></span></p>
                    <div class="about-readmore" onclick="myFunction()" id="myBtn">Read more</div>
                  <div class="readmore-border"></div>
                </div>
            </div><!-- end row --> 
    <div class="profile-border"></div>
 <?php
endwhile;
else:
endif;
?>

<script>
function myFunction() {
  var dots = document.getElementById("dots");
  var moreText = document.getElementById("more");
  var btnText = document.getElementById("myBtn");

  if (dots.style.display === "none") {
    dots.style.display = "inline";
    btnText.innerHTML = "Read more"; 
    moreText.style.display = "none";
  } else {
    dots.style.display = "none";
    btnText.innerHTML = "Read less"; 
    moreText.style.display = "inline";
  }
}
</script>

#more {display: none;} 

In this code: 在此代码中:

var dots = document.getElementById("dots");
var moreText = document.getElementById("more");
var btnText = document.getElementById("myBtn");

You are selecting the first occurrence of the element with id = "dots". 您正在选择id =“ dots”的元素的第一次出现。 That's always the 1st profile in your list. 这始终是您列表中的第一个配置文件。

1 1

One simple way to solve this would be: 解决此问题的一种简单方法是:

  1. change the <div id="dots"> to <div class="dots"> <div id="dots">更改为<div class="dots">
  2. parameterize this function with a count, then look up the nth occurrence of 用一个计数参数化此函数,然后查找第n次出现
  3. use document.querySelectorAll to look up all ".dots" elements, then expand the one using the passed in index to myFunction(index). 使用document.querySelectorAll查找所有“ .dots”元素,然后使用传递给myFunction(index)的索引来扩展一个元素。 You can use a php counter var to track index. 您可以使用php counter var来跟踪索引。

    <div class="about-readmore" onclick="myFunction(index)" id="myBtn">Read more</div>

2 2

Another, perhaps more fragile way to do it would be to look up and down the DOM to find the nearest profile to expand: 另一种可能更脆弱的方法是在DOM上下查找以找到最接近的配置文件进行扩展:

function myFunction(event) {
  var targetElementParent = event.target.parent;
  var moreText = targetElementParent.querySelector(".more");

  // The rest of the function could remain mostly the same

I say this is fragile because it relies on the structure of the DOM pretty heavily so if you change that, the function will break. 我说这很脆弱,因为它在很大程度上依赖于DOM的结构,因此,如果更改它,该功能将中断。 In this case, as in #1, you should change the ids "dots", "more" and "myBtn" to classes because those elements are repeated. 在这种情况下,如#1中一样,您应该将id“ dots”,“ more”和“ myBtn”更改为类,因为这些元素是重复的。

From MDN Id attribute page : MDN ID属性页面

The id global attribute defines a unique identifier (ID) which must be unique in the whole document id全局属性定义一个唯一标识符(ID),该标识符在整个文档中必须是唯一的

Ordinarily, you would expect to see that each block of text – each "profile," as you call them – would have a unique id in the HTML/DOM. 通常,您希望看到每个文本块(如您所说的每个“配置文件”)在HTML / DOM中都有唯一的id Let's say for the sake of example that the id of one of the blocks is "123." 举例来说,假设其中一个块的id为“ 123”。 The HTML for that profile would then be generated to contain onclick="myFunction(123)" . 然后将生成配置文件的HTML以包含onclick="myFunction(123)"

Now, each onClick is different – it supplies the id that the function is supposed to act upon, and this function should always see that getElementById returns a valid object because an element with that id should always be present. 现在,每个onClick都是不同的–它提供了该函数应该作用于其上的id ,并且此函数应始终看到getElementById返回有效对象,因为应始终存在具有该id的元素。 (If it doesn't, there's a bug in the generated HTML, and it should complain.) (如果没有,则说明生成的HTML中存在错误,应该投诉。)

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

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