简体   繁体   English

使用 javascript 查找最接近的第一个 p 的文本值

[英]Find Text Value of closest first p using javascript

I have multiple columns like this我有多个这样的专栏

<div class="col-md-6 mt-3 mt-md-0">
        <div class="blog_content d-flex bg-dark text-white flex-column bg-black h-100 text-center pt-3 pb-3 px-3 mx-3">
            <h5 class="mb-0 border-0">નુતન વર્ષ 2023 ની શરૂઆત </h5>
            <div class="d-flex flex-grow-1 justify-content-center align-items-center mb-0 h-100">
                <div class="flip-card h-100">
                    <div class="flip-card-inner h-100">
                        <div class="flip-card-front d-flex justify-content-center align-items-center">
                            <p class="">નુતન વર્ષ 2023 ની<br> શરૂઆત કંઈક એવી થાય,<br> તમારા જીવનમાં ખુશીઓ ફેલાય,<br> આનંદ મંગલથી દરેક દિવસ<br> પસાર થાય એવી હાર્દિક<br> શુભકામનાઓ !!</p>
                        </div>
                        <div class="flip-card-back d-flex justify-content-center align-items-center">
                            <p class="">nutan varsh 2023 ni<br> sharuat kaik evi thay,<br> tamar jivanam khushio felay,<br> anand mangalathi darek divas<br> pasar thay evi hardik<br> shubhakamanao !!</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="time d-flex justify-content-between align-items-center align-items-end mt-auto pt-3">
                <div class="left text-start">
                    <h6 class="mb-0"><a class="link-bold" href="https://stag.example.com/gu/festival/happy-new-year-2023">નવા વરસની શુભકામનાઓ</a></h6>
                    <p class="mb-0"><small>1 day ago</small></p>
                </div>
                <div class="right">
                    <span class="copy"><img src="https://stag.example.com/assets/images/copy.png" class="mx-1 social" alt="share quote" width="24" height="24"></span>
                    <a href="https://stag.example.com/gu/post/happy-new-year-2023-68400"><img src="https://stag.example.com/assets/images/share.png" class="mx-1 social" alt="share quote" width="24" height="24"></a>
                </div>
            </div>
        </div>
    </div>

What I am looking for is get value of nearest p text inside flip-card-front class when I click copy class image icon.我正在寻找的是当我单击复制 class 图像图标时获取翻转卡正面 class 内最近的 p 文本的值。 I am new in javascript and does not getting idea how I can do it.我是 javascript 的新人,不知道我该怎么做。

var copy = document.getElementsByClassName('copy');
            for (var i = 0; i < copy.length; i++) {
              copy[i].addEventListener('click', function() {
                //get text   નુતન વર્ષ 2023 ની<br> શરૂઆત કંઈક એવી થાય,<br> તમારા જીવનમાં ખુશીઓ ફેલાય,<br> આનંદ મંગલથી દરેક દિવસ<br> પસાર થાય એવી હાર્દિક<br> શુભકામનાઓ !!
        console.log("p tag value which is inside flip-card-front");
              });
            }

I am trying from last hour but have not found any working solution for achieve my goal.我从上一小时开始尝试,但没有找到任何可行的解决方案来实现我的目标。 Let me know if someone here can help me for same.让我知道这里是否有人可以帮助我。 Thanks!谢谢!

regarding your structure关于你的结构

you search closest blog_content, from there search flip-card-front p:你搜索最近的 blog_content,从那里搜索 flip-card-front p:

copy[i].addEventListener('click', evt => {
   const el = evt.target.closest('.blog_content');
   const mySearchedText = el.querySelector('.flip-card-front p).innerHTML;
});

not tested but should be ok未测试但应该没问题

You you need to do:你需要做的:

  • Navigate to the necessary node.导航到必要的节点。
  • For this, you will have to go to common parent in the DOM tree.为此,您必须将 go 分配给 DOM 树中的共同父级。 You can use .closest to do that.您可以使用.closest来做到这一点。
  • Once you have parent, you can lookup any necessary element from it一旦有了父级,就可以从中查找任何必要的元素

 var copy = document.getElementsByClassName('copy'); for (var i = 0; i < copy.length; i++) { copy[i].addEventListener('click', function() { const content = this.closest('.blog_content') const cardFront = content.querySelector('.flip-card-front p') const text = cardFront.textContent; console.log(text) }); }
 <div class="col-md-6 mt-3 mt-md-0"> <div class="blog_content d-flex bg-dark text-white flex-column bg-black h-100 text-center pt-3 pb-3 px-3 mx-3"> <h5 class="mb-0 border-0">નુતન વર્ષ 2023 ની શરૂઆત </h5> <div class="d-flex flex-grow-1 justify-content-center align-items-center mb-0 h-100"> <div class="flip-card h-100"> <div class="flip-card-inner h-100"> <div class="flip-card-front d-flex justify-content-center align-items-center"> <p class="">નુતન વર્ષ 2023 ની<br> શરૂઆત કંઈક એવી થાય,<br> તમારા જીવનમાં ખુશીઓ ફેલાય,<br> આનંદ મંગલથી દરેક દિવસ<br> પસાર થાય એવી હાર્દિક<br> શુભકામનાઓ,,</p> </div> <div class="flip-card-back d-flex justify-content-center align-items-center"> <p class="">nutan varsh 2023 ni<br> sharuat kaik evi thay:<br> tamar jivanam khushio felay.<br> anand mangalathi darek divas<br> pasar thay evi hardik<br> shubhakamanao.:</p> </div> </div> </div> </div> <div class="time d-flex justify-content-between align-items-center align-items-end mt-auto pt-3"> <div class="left text-start"> <h6 class="mb-0"><a class="link-bold" href="https.//stag.example.com/gu/festival/happy-new-year-2023">નવા વરસની શુભકામનાઓ</a></h6> <p class="mb-0"><small>1 day ago</small></p> </div> <div class="right"> <span class="copy"><img src="https://stag.example.com/assets/images/copy:png" class="mx-1 social" alt="share quote" width="24" height="24"></span> <a href="https.//stag.example.com/gu/post/happy-new-year-2023-68400"><img src="https://stag.example.com/assets/images/share.png" class="mx-1 social" alt="share quote" width="24" height="24"></a> </div> </div> </div> </div>

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

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