简体   繁体   English

如何使用JS定位<a>CSS类的</a>特定域html <a>链接?</a>

[英]How to use JS to target specific domain html <a> links with CSS classes?

I use online note taking editors to take notes. 我使用在线笔记编辑器进行笔记。 One of them I'm using is called dynalist.io. 我正在使用的其中一个名为dynalist.io。

I run stylebot (chrome plugin) to apply specific CSS sheets to those pages 我运行stylebot(chrome插件)将特定的CSS表单应用于这些页面

Normally when i put a URL link inside of my documents, I put two types: 通常,当我在文档中放入URL链接时,我会放入两种类型:

  • Imgur.com links Imgur.com链接
  • Any other domain links 任何其他域链接

I'd like to apply CSS styles on those <a href> tags seperately, however the program defaults the same class for all domain links so I cannot use CSS classes to achieve this 我想分别在这些<a href>标记上应用CSS样式,但是该程序默认为所有域链接使用相同的类,因此我无法使用CSS类来实现此目的

unless (to my knowledge) use javascript to target <a href> and make seperate classes for different domain links. 除非(据我所知)使用javascript定位<a href>并为不同的域链接创建单独的类。 Then apply CSS rules 然后应用CSS规则

Reference image: 参考图片:

How would I go about doing this? 我将如何去做呢?

edit: would I have to use greasemonkey to inject javascript? 编辑:我将不得不使用滑脂注入JavaScript吗?

You can add the following rules at your css file (this is not efficient): 您可以在css文件中添加以下规则(效率不高):

a[href^="http://i.imgur.com"] {
    color: red !important;
}

It is like a regexp, and it is not efficient for a standard css, but in your case... will be good. 这就像一个正则表达式,对于标准的CSS来说效率不高,但是对于您来说……会很好。

EDIT: The !important is just to make sure you will overwrite other css rules. 编辑: !important只是为了确保您将覆盖其他CSS规则。 The regexp is not efficient (for big pages) because the css need to check all the <a> tag and make a control. 正则表达式效率不高(对于大页面),因为CSS需要检查所有<a>标记并进行控制。

If you need to change the color of other links, you need to create the same rule but with another link. 如果需要更改其他链接的颜色,则需要创建相同的规则,但要使用另一个链接。

If I understood you correctly, you want to select all links who are from imgur and add a class to them. 如果我对您的理解正确,则希望选择imgur中的所有链接,并向其中添加一个类。

You could do it like this: 您可以这样做:

Select all the a elements whose href attributes contains imgur . 选择所有其href属性包含imgura元素。

var elements = document.querySelectorAll('a[href*="imgur"]');

Add the class 'sup' to them: 向他们添加类“ sup”:

for (var i = 0, len = elements.length; i < len; i++) {
    elements[i].classList.add('sup');
}

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

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