简体   繁体   English

如何使用Javascript编辑文字样式

[英]How to edit text style with Javascript

I'm trying to make a button create new entries in a list that display similar to this: 我试图使一个按钮在显示类似于以下内容的列表中创建新条目:

"#1 new Click Me" “#1个新的Click Me”

Except I want to make "Click Me" to show up as yellow text in a black box, and then I want to make the black box disappear and the text turn brown on mouseover. 除了我要使“ Click Me”在黑框中显示为黄色文本,然后要使黑框消失并且鼠标悬停时文本变为棕色。 I've been able to make the list appear, but don't know how to edit the style of the text to make it appear the way I want to. 我已经能够使列表显示出来,但是不知道如何编辑文本样式以使其以我想要的方式显示。 The most code I think I need to give for this is this: 我认为我需要为此提供的最多代码是:

var li = document.createElement("li");
var liBody = document.createTextNode("#"+numOfNewCMs+
     " new " + newClickMe);
li.appendChild(liBody);

And then I insert li into the list. 然后我将li插入列表。

I figure I should make newClickMe a variable and edit that and then put it next to the rest of the text in the liBody variable, and I figure the HTML span element is the best way to do that, except I don't even know quite what the span element really does. 我认为应该将newClickMe设置为一个变量,然后对其进行编辑,然后将其置于liBody变量中其余文本的旁边,并且我认为HTML span元素是实现此目的的最佳方法,除非我什至不知道span元素真正的作用。 How do I go about editing the style of that particular string? 我该如何编辑该特定字符串的样式? I can't get around to figuring out how (if I even can) make the text turn brown on mouseover until I do so. 我无法弄清楚如何(如果可以的话)使鼠标悬停时文本变成棕色,直到我这样做。

Never met CreateTextNode, but i guess liBody.style.fontSize="12px" should help. 从未见过CreateTextNode,但我想liBody.style.fontSize="12px"应该会有所帮助。 And other properties such as 'fontWeight,color,fontStyle...' 以及其他属性,例如“ fontWeight,color,fontStyle ...”

HTML elements have a style property that can be used to apply CSS styles to them. HTML元素具有style属性,可用于将CSS样式应用于它们。

For example: 例如:

var newClickMe = document.createElement("span");
newClickMe.style.backgroundColor = "#000000";
newClickMe.style.color = "#FFFF00";
newClickMe.innerText = "Click Me";

var li = document.createElement("li");
var liText = document.createTextNode("#"+numOfNewCMs+
     " new ");

li.appendChild(liText);
li.appendChild(newClickMe);

Will make the list item have a black background with yellow text. 将使列表项具有带有黄色文本的黑色背景。

For more details on the style property, MDN has a great section on it: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style 有关style属性的更多详细信息,MDN上有一个不错的部分: https : //developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style

And here is a reference page to translate CSS properties into their JavaScript equivalent: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference 以下是将CSS属性转换为等效JavaScript的参考页: https : //developer.mozilla.org/en-US/docs/Web/CSS/CSS_Properties_Reference

I'm assuming you want the styles to change on mouseOver. 我假设您希望样式在mouseOver上更改。 I just changed the styles using css' :hover. 我只是使用css':hover更改了样式。 Is this what you had in mind? 这就是您的想法吗?

 var numOfNewCMs=1; function generateLi(){ var li = document.createElement("li"); var liBody = document.createTextNode("#"+numOfNewCMs+ " new "); var sp = document.createElement("span"); var spBody = document.createTextNode("Click Me"); sp.setAttribute("id", "sp"+numOfNewCMs); sp.setAttribute("onmouseover", "highlight("+numOfNewCMs+")"); sp.setAttribute("onmouseout", "highlight2("+numOfNewCMs+")"); sp.style.backgroundColor='black'; sp.style.color='yellow'; sp.appendChild(spBody); li.appendChild(liBody); li.appendChild(sp); lis.appendChild(li); numOfNewCMs++; } function highlight(id){ var element= document.getElementById('sp'+id); element.style.backgroundColor='white'; element.style.color='brown'; } function highlight2(id){ var element= document.getElementById('sp'+id); element.style.backgroundColor='black'; element.style.color='yellow'; } 
 li{ margin-bottom:20px; } li > span{ padding:5px; margin-bottom:10px; } li:hover > span{ color:brown; background-color:white; } 
 <button onclick="generateLi()">Click me</button> <div id="lis" style="margin-top:20px;"></div> 

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

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