简体   繁体   English

如何使用innerHTML更改文本不透明度?

[英]How to change text opacity using innerHTML?

Hi i was just wondering if there was a way to change the opacity of text using innerHTML almost as a container... 嗨,我只是想知道是否有一种方法可以将innerHTML几乎用作容器来更改文本的不透明度。

var x = document.getElementById("myId");
x.innerHTML.style.opacity = .5;

??? ??? maybe something like that? 也许像那样的东西? I had an idea to have a different class and just add that class to specific HTML but im trying to grab the html of an element, change the text opacity to a lighter shade, delete it from the element but keep it in a variable, add in new html, and then add the original html but i want the new html to be darker than the original so i cant change the opacity of the element otherwise it will effect all the text in the element. 我有一个不同的类,只是将该类添加到特定的HTML中,但是我试图抓取元素的html,将文本不透明度更改为较浅的阴影,将其从元素中删除,但将其保留在变量中,添加在新的html中,然后添加原始的html,但是我希望新的html比原始的要暗,所以我不能更改元素的不透明度,否则它将影响元素中的所有文本。 If anyone has any suggestions it would be greatly appreciated. 如果有人有任何建议,将不胜感激。 I am trying to use only HTML, CSS, and Javascript so im trying to avoid any added libraries if i can help it. 我正在尝试仅使用HTML,CSS和Javascript,因此,如果可以的话,我会尽量避免添加任何库。 thank you. 谢谢。

You could changes the opacity of all childrens 您可以更改所有孩子的不透明度

var childrens = document.getElementById("myId").children;

for(var i = 0, length = childrens.length; i < length; i++) {
    childrens[i].style.opacity = 0.5;
}

No far ... like that : 不远...像这样:

var x = document.getElementById("myId");
x.style.opacity = .5;

you can use x.style.opacity = 0.5 您可以使用x.style.opacity = 0.5

update : since you need to change only one part of the text ,, you should do something like this 更新:由于您只需要更改文本的一部分,因此您应该执行以下操作

x= document.getElementById("id");
x.innerHtml = "not important text <div id='important'>Important</div>";
document.getElementById("important").style.opacity = 0.5;

Why do you want to do something like this?? 你为什么要做这样的事情? can you explain more ,, So we can help you ? 您能否解释更多,,那么我们可以为您提供帮助吗?

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

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