简体   繁体   English

修改jQuery函数(抓取每个DOM元素)

[英]Modify jQuery function (grab each DOM element)

This is function that I use to change the alpha color of a DOM's element/s. 这是我用来改变DOM元素的alpha颜色的函数。

function RGBA(e, alpha) { //e = jQuery element, alpha = background-opacity
    b = e.css('backgroundColor');
    e.css('backgroundColor', 'rgba' + b.slice(b.indexOf('('), ( (b.match(/,/g).length == 2) ? -1 : b.lastIndexOf(',') - b.length) ) + ', '+alpha+')');
}

I found this function here Changing background opacity of div using RGBa . 我发现这个函数在这里使用RGBa改变div的背景不透明度

I am using wordpress therefore I want this function to grab each div within my loop of a certain html class. 我正在使用wordpress因此我想要这个函数来获取某个html类的循环中的每个div。

Right now this function works well but it changes the alpha color of first category and loops that color through all categories. 现在这个功能运行良好,但它改变了第一个类别的alpha颜色,并在所有类别中循环着色。 If you need more details then: I have a wordpress plugin that assigns background color property to categories and then outputs it on live site. 如果您需要更多详细信息:我有一个wordpress插件,它将背景颜色属性分配给类别,然后在实际站点上输出。

My code looks something like this: 我的代码看起来像这样:

<p class="x" style="background-color: <?php echo $rl_category_color; ?></p>
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; }

Now I target the x class in following function 现在我在以下函数中定位x类

RGBA(jQuery('.x'), 0.2);

If that helps you then here you have visual on my situation. 如果这可以帮助你,然后在这里 ,你对我的情况的视觉。

And problem is it grabs the first category color and assigns it to every p with x class ( it should loop throught every single one and output it separately I think or it should target the p element by its full classname, I've added the wordpress post id function to make every class unique by adding an post ID to every single x class element so every x class element grabbed by its full classname would be different but thats just my thoughts ). 问题是它抓住第一类颜色并将其分配给每个具有x类的p(它应该循环遍历每一个并分别输出它我认为或它应该通过其完整的类名来定位p元素,我添加了wordpress post id函数通过向每个单独的x类元素添加一个帖子ID来使每个类都是唯一的,因此每个x类元素被其完整的类名称抓取会有所不同,但这只是我的想法)。

So my goal with this is in following image Its just part of the design why I want to make it opaque. 所以我的目标是在下面的图像中它只是设计的一部分,为什么我想让它变得不透明。

Sorry if I violate rules of asking a question, I am not an expert in any of these languages. 很抱歉,如果我违反了提问的规则,我就不是这些语言的专家。

What I should do is this: 我应该做的是:

$(".x").each(function() {
  RGBA($(this), 0.2);
}

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

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