0

我试图提取GTM中的产品描述标记的全部文本(部署结构化数据,yada yada)。我面临的问题是,我试图提取的文本的一半存在于产品页面上的“Read More ...”链接中。在“ReadMore ...”触发器中连接文本

页面上的元素的布局已呈现后是:

<p class="product-description"> 
"This is the first part of the description of this product! It is read, warm," 
    <span class="morecontent" display="inline"> //inline when expanded, none when clicked 
    <span class="readmore-description"> 
     "fuzzy, and black." 
    </span> 
    <a class="more-link">&nsbp;Less</a> //&nsbp;Less when expanded, &nsbp;Read More.. when clicked 
    </span> 
</p> 

我没有得到远......所有到目前为止,我已经试过是:

function() { 

    var description = document.querySelector("p.product-description"); 

    return description; 

} 

我想得到“这是这个产品的第一部分描述!它被读取,温暖,模糊和黑色。”背部。

谢谢!

回答

0

我在隐藏的内容上使用了一个类。当该类存在时,隐藏内容,当内容不存在时显示内容。把它看作是一个开关。

$('.more-link').on('click', function(e){ 
 
    e.preventDefault(); 
 
    $('.morecontent').toggleClass('hidden'); 
 
    if ($('.morecontent').hasClass('hidden')) { 
 
    $('.more-link').html('More'); 
 
    } 
 
    else { 
 
    $('.more-link').html('Less'); 
 
    } 
 
});
.hidden { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p class="product-description"> 
 
"This is the first part of the description of this product! It is read, warm," 
 
    <span class="morecontent hidden" display="inline"> 
 
    <span class="readmore-description"> 
 
     "fuzzy, and black." 
 
    </span> 
 
    </span> 
 
    <a href="#" class="more-link">More</a> 
 
</p>

+0

感谢这个 - 我认为这是类似于页面是如何架构已经。你知道我怎样才能用一个函数来提取完整​​的描述(不是分成两部分)吗? 'function(){ var description = document.querySelector(“p.product-description”); return description; }'是我现在使用的,但当然不起作用。我认为脚本需要以某种方式连接。 – PercentSharp

+0

仅当您分别提取它们,然后将它们连接到一个字符串中。 – zsawaf