最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Pure javascript alternative for jQuery .prev()? - Stack Overflow

programmeradmin3浏览0评论

I have this jQuery code:

$(".q-block-container").prev(".sub-block-container").css("border-bottom","none");

I need a pure JavaScript equivalent, whereby I can select the previous sibling ONLY if it matches the selector (in this case .sub-block-container).

For example, lets say I have a list and each item in the list has a border-bottom style. Depending in what the sibling is before a particular list item, that should determine whether or not the border should be applied:

<ul>
    <li class="q"></li>
    <li class="q"></li>
    <li class="q"></li>
    <li class="s"></li>
    <li class="s"></li>
    <li class="q"></li>
    <li class="s"></li>
    <li class="q"></li>
</ul>

In this example, a border must NOT appear on the previous sibling <li> element if:

  • the element is q and the previous sibling is s

  • the element is s and the previous sibling is q

  • the element is s and the previous sibling is s

I have this jQuery code:

$(".q-block-container").prev(".sub-block-container").css("border-bottom","none");

I need a pure JavaScript equivalent, whereby I can select the previous sibling ONLY if it matches the selector (in this case .sub-block-container).

For example, lets say I have a list and each item in the list has a border-bottom style. Depending in what the sibling is before a particular list item, that should determine whether or not the border should be applied:

<ul>
    <li class="q"></li>
    <li class="q"></li>
    <li class="q"></li>
    <li class="s"></li>
    <li class="s"></li>
    <li class="q"></li>
    <li class="s"></li>
    <li class="q"></li>
</ul>

In this example, a border must NOT appear on the previous sibling <li> element if:

  • the element is q and the previous sibling is s

  • the element is s and the previous sibling is q

  • the element is s and the previous sibling is s

Share Improve this question edited Apr 29, 2017 at 23:26 Abraham Murciano Benzadon 1,72318 silver badges35 bronze badges asked Apr 29, 2017 at 23:02 JoeTideeJoeTidee 26.2k29 gold badges113 silver badges163 bronze badges 10
  • May I ask why? This doesn't seem like an efficient thing to do. – Derek 朕會功夫 Commented Apr 29, 2017 at 23:04
  • What's the efficient thing to do? – JoeTidee Commented Apr 29, 2017 at 23:06
  • I have taken a look at your example. In this case, I assume your rows are programmatically generated. What is usually done is to assign classes directly to the elements that you wish to have certain property, ie in this case you would add a class with-border to elements that should have a border before they are put into the DOM. – Derek 朕會功夫 Commented Apr 29, 2017 at 23:21
  • 1 Might help stackoverflow./questions/574904/… – Robert Commented Apr 29, 2017 at 23:30
  • 1 The logic may be easier if the decision to apply a border is based on paring the content of an element with that of its next sibling. – traktor Commented Apr 29, 2017 at 23:37
 |  Show 5 more ments

2 Answers 2

Reset to default 6

Try this if your element .sub-block-container will have only that single class.

var elem = document.getElementsByClassName("q-block-container");
for (i=0; i<elem.length; i++) {
    var prev = elem[i].previousElementSibling;
    if (prev.className == "sub-block-container") {
        prev.style.borderBottom = "none";
    }
}

If your element may have more than one class, use this instead:

var elem = document.getElementsByClassName("q-block-container");
for (i=0; i<elem.length; i++) {
    var prev = elem[i].previousElementSibling;
    if (prev.classList.contains("sub-block-container")) {
        prev.style.borderBottom = "none";
    }
}

Maybe try this :

if (!Element.prototype.matches) {
    Element.prototype.matches = 
        Element.prototype.matchesSelector || 
        Element.prototype.mozMatchesSelector ||
        Element.prototype.msMatchesSelector || 
        Element.prototype.oMatchesSelector || 
        Element.prototype.webkitMatchesSelector ||
        function(s) {
            var matches = (this.document || this.ownerDocument).querySelectorAll(s),
                i = matches.length;
            while (--i >= 0 && matches.item(i) !== this) {}
            return i > -1;            
        };
}

var elements = document.querySelectorAll('.q-block-container');

for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    var previous = element.previousElementSibling;
    if(previous.matches('.sub-block-container')){
        previous.style.borderBottom = 'none';
    }
}​

So it is quite straightforward, you iterate over all the .q-block-container and if one's previous sibling matches the .sub-block-container selector, it changes its border-bottom

Edit :

Thank you Abraham Murciano Benzadon for pointing out an error in the code (semi-colons aren't required in JS tho, but it's best to keep the same code style)

Edit 2 :

Fixed the code to what you want

发布评论

评论列表(0)

  1. 暂无评论