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