【问题标题】:How to get the Dynamic values of the Title如何获取标题的动态值
【发布时间】:2021-08-08 23:27:29
【问题描述】:

我有一个代码,其中按钮和标题位于同一个 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>

我正在使用这样的代码:

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

要求: 一旦链接(a.cmpl-teaserd__action-link)被点击,我想获得容器的Title1(div.cmpl-teaserd__pretitle)Title2(div.cmpl-teaserd__title wow animated)

请提供您的反馈以获得价值

【问题讨论】:

    标签: javascript jquery dynamic


    【解决方案1】:

    您可以遍历所选 div 内的所有 div,然后维护一个计数器以仅打印您要打印的 div。它可以更好地实施。 这可能会有所帮助。

        $(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'));
                }
            });
      
       });
    

    【讨论】:

    • 感谢您的回复,我已经尝试过这段代码,但是这不起作用。
    • 您能否发布您遇到的错误。
    猜你喜欢
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多