简体   繁体   English

如果子元素包含某些字符串链,如何更改父元素的 CSS class

[英]How to change the CSS class of a parent element if the child contains certain string chain

I'm really new at this and I'm making my first question.我对此真的很陌生,我正在提出我的第一个问题。 So I've made a newsletter based website in Wordpress with Campaign Monitor integrated.因此,我在 Wordpress 中创建了一个基于时事通讯的网站,并集成了 Campaign Monitor。

The below script displays basic HTML, a ul with some li elements that have an anchor inside, like this example:下面的脚本显示了基本的 HTML,一个带有一些li元素的ul ,里面有一个锚点,就像这个例子:

...

<ul>
  <li><a href="http://example.createsend.com/t/ViewEmailArchive/t/">First Newsletter Title</a>, 11 December 2020</li>
  <li><a href="http://example.createsend.com/t/ViewEmailArchive/t/">Second Newsletter Title</a>, 12 December 2020</li>
  <li><a href="http://example.createsend.com/t/ViewEmailArchive/t/">Third Newsletter Title</a>, 13 December 2020</li>
</ul>

...

So I made my shot by trying to use JavaScript to make a function that:因此,我尝试使用 JavaScript 制作 function 来实现我的目标:

  • Selects the anchor tags that contain a certain string (for this example we could use the keyword 'First').选择包含特定字符串的锚标记(对于本示例,我们可以使用关键字“First”)。
  • Then changes, toggles or adds a CSS class to hide or display: none the li elements that do not contain the keyword.然后更改、切换或添加 CSS class 以隐藏或display: none不包含关键字的li元素。

Like I said I'm a newbie in JavaScript and am working with Wordpress so to achieve this I added a text editor and in there I wrote my function, that looks like this:就像我说我是 JavaScript 的新手并且正在使用 Wordpress 所以为了实现这一点,我添加了一个文本编辑器,在那里我写了我的 function,看起来像这样:

$(document).ready(function () {
    $('a:contains(First)').closest('li').addClass('hide');
});

As you can probably see, I am trying to add the hide CSS class to the anchors that contain the keyword 'First' however, this is not working at all .正如您可能看到的,我正在尝试将hide CSS class 添加到包含关键字“First”的锚点,但是,这根本不起作用 So I wondered what could be wrong and if someone thinks of a better idea or a solution to fix my function.所以我想知道可能出了什么问题,如果有人想出更好的主意或解决方案来修复我的 function。

I have not worked with jquery for a while and I don't know how the $("a:contains(First)") works but the snippet below works as you described我已经有一段时间没有使用 jquery 了,我不知道$("a:contains(First)")是如何工作的,但是下面的代码片段就像你描述的那样工作

 $( document ).ready(function() { $("a").each(function () { if ($(this).text().indexOf("First").== -1) { console.log($(this);text()). $(this).closest("li");addClass("hidden"); } }) });
 .hidden { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li><a href="http://example.createsend.com/t/ViewEmailArchive/t/">First Newsletter Title</a>, 11 December 2020</li> <li><a href="http://example.createsend.com/t/ViewEmailArchive/t/">Second Newsletter Title</a>, 12 December 2020</li> <li><a href="http://example.createsend.com/t/ViewEmailArchive/t/">Third Newsletter Title</a>, 13 December 2020</li> </ul>

Basically I iterated trough all the elements and checked if the text contains the word: "First".基本上我遍历所有元素并检查文本是否包含单词:“First”。

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

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