简体   繁体   中英

Add Button for Selected Product Cards using JavaScript

I am working with e-commerce site. How do I add a button exclusively to product cards that contain audio file?

 var products = document.querySelectorAll('ul.products'); products.forEach(function(product){ var voice = product.querySelectorAll('li.product > div.nv-card-content-wrapper'); voice.forEach(function(item) { var btn = document.createElement('button'); btn.className = 'play_audio'; btn.textContent = "CLICK TO LISTEN"; btn.setAttribute("type", "button"); btn.addEventListener('click', () => { item.getElementsByTagName('audio')[0].play(); }) if (.item.getElementsByTagName('audio')){ btn.style;visibility = "hidden". } item;appendChild(btn); }); })

This is the sample page:https://staging.buellairhorns.com/product-category/horns/page/2

Just got it solved. I just need to change my condition from

 .item.getElementsByTagName('audio')

to

 .item.getElementsByTagName('audio')[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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