简体   繁体   English

如何忽略具有特定CSS类的HTML组件

[英]How to ignore HTML component with specific CSS class

i have many links in page but few links has few specific css class attached. 我在页面中有很多链接,但很少有链接附带一些特定的CSS类。 i want to avoid those link which has .noclass attached. 我想避免那些链接有.noclass的链接。 the below code solve my problem. 下面的代码解决了我的问题。

<a href="#" class="noclass">Link with No class1</a><br/>
<a href="#" class="noclass">Link with No class2</a><br/>
<a href="#" >Link without No class</a><br/><br/>

<div id="footer_column6">
  <a href="#" class="noclass">Link with No class</a><br/>
  <a href="#" class="">Link without No class</a><br/>
  <a href="#" class="">Link without No class</a>
</div>
$(document).ready(function() {
    var hyperLink = $("a:not(.noclass)");
    $.each(hyperLink, function(index, value) {
      var href = $(value).attr("href");
      //Test code to update the back ground
      $(value).css("background-color", "red");
      $(value).attr("href", "/" + countrycode + (href == '/' ? '' : href));
      href = $(value).attr("href");
    });
});

when the scenario is bit different like i want to ignore anything with css class .noclass then above code is not working. 当场景有点不同,例如我想使用CSS类.noclass忽略任何内容时,上述代码将无法正常工作。

suppose if few links are there in div where noclass has been attached to div and i do not want to iterate links in those div which has no class attached. 假设在div中没有​​链接的类中没有连接,并且我不想在没有类的div中迭代链接。

so if noclass is attached directly with links or may be noclass may attached to parent like div or span. 因此,如果noclass通过链接直接附加或可能是noclass,则可以将其附加到父级,例如div或span。 in this case i do not want to iterate links if its parent has no class attached. 在这种情况下,如果其父级未附加任何类,则我不想迭代链接。 so tell me what little modification i have to add in above jquery code. 所以告诉我我必须在上面的jquery代码中添加些什么修改。

thanks 谢谢

I have revised my previous code, I realised there was a logic flaw and also didn't like that it relied on adding an extra outer div to make it work. 我已经修改了之前的代码,意识到存在逻辑缺陷,也不希望它依赖于添加一个额外的外部div使其起作用。 This version selects all links initially and then from within your .each, it tests if the link as the .noclass or has an ancestor with .noclass: 此版本首先选择所有链接,然后从您的.each中选择所有链接,它会测试该链接是否为.noclass或祖先带有.noclass:

$(document).ready(function() {
    var hyperLink = $("a");
    $.each(hyperLink, function(index, value) {
        if($(this).hasClass('noclass') || $(this).parents('.noclass').length) {
        return;
      }
      var href = $(value).attr("href");
      //Test code to update the back ground
      $(value).css("background-color", "red");
      $(value).attr("href", "/" + countrycode + (href == '/' ? '' : href));
      href = $(value).attr("href");
    });
});

This is more robust as it also works with links at the root of the page, and for any kind of wrapper element (div, span, section, article, etc). 这更加健壮,因为它还可以处理页面根目录中的链接以及任何类型的包装器元素(div,span,section,article等)。

Here's a Fiddle example (note, I had to comment out the countrycode line to make the Fiddle work, as countrycode is not defined). 这是一个Fiddle示例(请注意,由于未定义countrycode,我必须注释掉countrycode行才能使Fiddle工作)。

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

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