简体   繁体   English

如何获取标题的动态值

[英]How to get the Dynamic values of the Title

I have a code where the button and title are in same div container like this:我有一个代码,其中按钮和标题位于同一个 div 容器中,如下所示:

<div class="cmpl-teaserd__content">
        <div class="cmpl-teaserd__pretitle">Featured Sample Title</div>        
    <div class="cmpl-teaserd__title wow animated" style="visibility: visible;">
        <h2><b>Borcher</b> <span style="font-weight: normal;">Collection</span></h2>
    </div>      
    <div class="cmpl-teaserd__description">
        <p>Sample text Sample text Sample text Sample text</p>
    </div>        
    <div class="cmpl-teaserd__action-container">        
    <a class="cmpl-teaserd__action-link" href="#.html" target="_self" title="">View Collection</a>
 </div>

Am using the Code like this:我正在使用这样的代码:

$(document).on("click","a.cmpl-teaserd__action-link",function(){
    console.log("Title:" + $(this).text() + " Link Clicked");
  
});

Requirement: I want to get the Title1 (div.cmpl-teaserd__pretitle) & Title2 (div.cmpl-teaserd__title wow animated) of the container once the link (a.cmpl-teaserd__action-link) got clicked.要求:单击链接(a.cmpl-teaserd__action-link)后,我想获取容器的Title1 (div.cmpl-teaserd__pretitle)Title2 (div.cmpl-teaserd__title wow animated)

Kindly provide your feedback to get the value请提供您的反馈以获得价值

You can loop through all the divs inside the selected div and then maintain a counter to print just the div you want to print.您可以遍历所选 div 内的所有 div,然后维护一个计数器以仅打印您要打印的 div。 It can be better implemented.它可以更好地实施。 this might help.这可能会有所帮助。

    $(document).on("click","a.cmpl-teaserd__action-link",function(){
        var count = 1;
        $('div.cmpl-teaserd__content div').each (function (count) {
            count++;
            if(count<3){
                console.log("Title"+count +" "+  $(this).attr('class'));
            }
        });
  
   });

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

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