简体   繁体   English

Javascript,加载文档时的数据过滤器

[英]Javascript, data-filter on loading document

I have a site, where some divs are being sorted via data-filters. 我有一个网站,其中一些div通过数据过滤器进行排序。

<li><a href="#" class="active" data-filter=".xyz">XYZ</a></li>

Now when loading the Document all divs show up. 现在,在加载文档时,所有div都会显示出来。 How can I achieve it, that only the div with class "xyz" shows up? 我该如何实现,仅显示类“ xyz”的div?

Hope someone can help me, really not getting it right now. 希望有人能帮助我,真的现在还不能解决。

Edit: Sorry for inputting so few code. 编辑:很抱歉输入了这么少的代码。

<ul class="filter">
    <li><a href="#" class="active" data-filter=".nature">Nature</a></li>
    <li><a href="#" data-filter=".logo">Logo</a></li>
    <li><a href="#" data-filter=".brochures">Brochures</a></li>
    <li><a href="#" data-filter=".business-card">Business Card</a></li>
</ul>
<div class="nature">...</div>
<div class="logo">...</div>

Now every div is shown on pageload. 现在,每个div都显示在页面加载量上。 I'd only like the first div (.nature) to be shown. 我只希望显示第一个div(.nature)。 Filtering is done by?? 过滤是通过? I'd guess jquery. 我猜jquery。

Assuming that by "show up" you mean display, you can control this using css: 假设“显示”是指显示,则可以使用css进行控制:

[data-filter = '.xyz'] {
  display: none;
}

That will match all elements that have an attribute called data-filter with an exact value of .xyz . 这将匹配有一个属性,叫做所有元素data-filter精确.xyz If you're going to use this attribute much like the class attribute, where classes are separated by whitespace, you can use the |= attribute selector instead: 如果要像使用class属性(由空格分隔类)那样使用此属性,则可以改用|=属性选择器:

[data-filter ~= '.xyz'] {
  display: none;
}

Here you can see it in a working demo . 在这里,您可以在正在运行的演示中看到它。

Hope this helps! 希望这可以帮助!

If I understood correctly, you need to hide any element having a data-filter attribute's value (a CSS selector) that doesn't match the element on which the attribute is on? 如果我理解正确,您需要隐藏任何具有data-filter属性值的元素(CSS选择器),该值与该属性所在的元素不匹配?

You could do something like this: 您可以执行以下操作:

HTML 的HTML

<div class="xyz" data-filter=".xyz">should show</div>
<div class="no-xyz" data-filter=".xyz">should not show</div>
<div class="no-xyz" data-filter=".xyz">should not show</div>

JS JS

window.onload = function () {
    var filteredEls = document.querySelectorAll('[data-filter]'),
        i = 0,
        len = filteredEls.length,
        frag = document.createDocumentFragment(),
        el, elClone;

    for (; i < len; i++) {
        el = filteredEls[i];

        elClone = el.cloneNode(false);

        frag.appendChild(elClone);

        if (!frag.querySelector(el.getAttribute('data-filter'))) {
            el.style.display = 'none';
        }

        frag.removeChild(elClone);
    }

};

EDIT: Looks like you edited the question and I misunderstood it the first time. 编辑:好像您编辑了问题,但我第一次误解了。

If you are using jQuery and already have a plugin that does the filtering when you click on those filter elements, but the plugin doesn't initially hide any elements, you can probably simply trigger a click event on the initial filter. 如果您使用的是jQuery,并且已经有一个插件在单击这些过滤器元素时进行过滤,但是该插件最初并未隐藏任何元素,那么您可以简单地在初始过滤器上触发click事件。

$(function () {
    $('ul.filter li:first-child > a').trigger('click');
});

I found the Solution. 我找到了解决方案。 I only did google the false things. 我只用谷歌做虚假的事情。 Solution was right here. 解决方案就在这里。 How to activate JS data-filter when page loads? 页面加载时如何激活JS数据过滤器? Thanks to you all for your answers! 谢谢大家的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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