【问题标题】:How to manipulate specific div inside a for-loop of EJS?如何在 EJS 的 for 循环中操作特定的 div?
【发布时间】:2022-01-24 04:28:40
【问题描述】:

在谷歌上搜索了几个小时,我想在 onClick 事件期间更改一个元素的 svg 的颜色,结果它要么设置 for 循环中的所有元素的样式,要么只设置第一个元素的样式。我在这里添加了我的 ejs 和 toggleSvg() js 脚本。希望你能帮助我。

ejs sn-p: (在此处查找“svg”)

<div class="max-w-5xl mt-14 mx-auto sm:max">
          <% posts.forEach(post=> { %>
            <div class="my-20">
                <div class=" px-2 mb-2 flex items-center justify-between">
                    <div class="flex items-center">
                        <div class="border border-gray-300 p-1 rounded-full w-10 h-10 flex items-center bg-white">
                            <img
                            src="<%= post.merchant.image %>"
                            alt="..."
                            class="w-10"
                            loading="lazy"
                                />
                        </div>
                        
                        <p class="pl-5"><%= post.merchant.name %></p>
                    </div>
                    <div>
<!--svg here!-->
                            <svg id="test" xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 cursor-pointer" fill="none" viewBox="0 0 24 24" stroke="currentColor" onclick="toggleSvg()" >
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 5a2 2 0 012-2h10a2 2 0 012 2v16l-7-3.5L5 21V5z"/>
                              </svg>
                    </div>
                </div>
                <!--image carousell-->
                <div class="swiper mySwiper">
                    <div class="swiper-wrapper">
                        <div class="swiper-slide w-10 h-20 bg-black ">
                            <div class="justify-center flex ">
                                <img
                                src="<%= post.imageUrl[0] %>"
                                alt="..."
                                class="h-72"
                                loading="lazy"
                              />
                            </div>
                        </div>
                        <div class="swiper-slide bg-black">
                            <div class="justify-center flex  bg-black">
                                <img
                                src="<%= post.imageUrl[1] %>"
                                alt="..."
                                class="h-72"
                                loading="lazy"
                              />
                            </div>
                        </div>
                        <div class="swiper-slide bg-black">
                            <div class="justify-center flex bg-black">
                                <img
                                src="<%= post.imageUrl[2] %>"
                                alt="..."
                                class="h-72"
                                loading="lazy"
                              />
                            </div>
                        </div>
                        <div class="swiper-slide bg-black">
                            <div class="justify-center flex bg-black">
                                <img
                                src="<%= post.imageUrl[3] %>"
                                alt="..."
                                class="h-72"
                                loading="lazy"
                              />
                            </div>
                        </div>
                    </div>
                    <div class="swiper-button-next"></div>
                    <div class="swiper-button-prev"></div>
                    <div class="swiper-pagination"></div>
                </div>
                
                <div class="">
                    <p><span class="font-bold pr-3"><%= post.merchant.name %></span><%= post.description %></p>
                </div>
              </div>
          <% }) %>
      </div>`enter code here`

toggleSvg() js:

<script>
        function toggleSvg() {
            svgElem = document.getElementById("test");
            if(svgElem.style.fill === 'red'){
                svgElem.style.fill = 'none';
            }else{
                svgElem.style.fill = 'red';
            }
        }
</script>

【问题讨论】:

    标签: for-loop svg ejs


    【解决方案1】:

    您在循环中使用id 只能选择一个元素,如果要为所有 svg 着色,可以使用class,如果要为特定的 svg 着色,可以这样做

    onclick="toggleSvg(this)"
    

    这样你在点击时传递当前元素

    <script>
            function toggleSvg(svgElem ) {
                if(svgElem.style.fill === 'red'){
                    svgElem.style.fill = 'none';
                }else{
                    svgElem.style.fill = 'red';
                }
            }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2020-09-14
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多