Equivalent of jQuery's contents().find() chained methods in pure JS

牧云@^-^@ 提交于 2020-04-18 05:45:46

问题


So I've been trying to do the following bit of code without jQuery:

$(".parent-class").contents().find(".child-class").css("color", "red");

I'm trying to edit the styles of an embedded Twitter feed and I could only do that by getting the module's child nodes using this snippet: $(".twitter-timeline").find(".timeline-Tweet-text").css("margin-bottom", "-10px"); for whatever reason. It is necessary that the pure JS code mimics this functionality. My full js function is:

// Change the style of the tweets after the module loads.
window.addEventListener('load', function () {
   $(".twitter-timeline").contents().find(".timeline-Tweet-text").css("margin-bottom", "-10px");
});

Thanks for taking the time to read.


回答1:


You can try the following way:

document.querySelectorAll(".twitter-timeline .timeline-Tweet-text").forEach(function(el){
    el.style.marginBottom = "-10px";
 });


来源:https://stackoverflow.com/questions/61099280/equivalent-of-jquerys-contents-find-chained-methods-in-pure-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!