提问人:Line One 提问时间:3/24/2023 最后编辑:Line One 更新时间:3/24/2023 访问量:27
使用带有 jQuery/JS 的通配符按类选择元素
Select elements by class using wildcard with jQuery/JS
问:
我已经看到了处理在CSS中选择元素的答案,我想知道是否可以在jQuery中使用通配符用一行执行以下操作?
jQuery('.youtube-video-1')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
jQuery('.youtube-video-2')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
jQuery('.youtube-video-3')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
我试过:
jQuery('.youtube-video-\\S*')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
jQuery('[class^=youtube-video-]')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
答: 暂无答案
评论
youtube-video
class="youtube-video-1"
document.querySelectorAll('youtube-video-class').forEach(vidFrame => vidFrame.contentWindow.postMessage(.....))