简体   繁体   English

如何在段落中设置动态内容的样式?

[英]How do I style dynamic contents inside a paragraph?

If I'm doing this in JavaScript : 如果我在JavaScript中执行此操作:

.......
if (!isNaN(num))
    document.getElementById("isNum").innerHTML = num +" is a number";
......

Then HTML : 然后HTML:

<div>
    <button onclick="checknum()">Insert anything : </button>
    <p id="isNum"></p>  
</div>

The output (suppose input is 3): 输出(假设输入为3):

3 is a number 

So the question is : 所以问题是:

How can I color, maybe by css, the variable into Javascript, the output something like: 我怎样才能通过css将变量着色为Javascript,输出内容如下:

3 is a number 3 is a number

You could wrap your num by span : 您可以按span来包装num

document.getElementById("isNum").innerHTML = '<span>'+num+"</span> is a number";

Then add a css rule for this specific span inside paragraph that have id #isNum : 然后在ID为#isNum段落中为此特定范围添加一个CSS规则:

#isNum span{
    font-weight: bold;
    color: green;
}

Hope this helps. 希望这可以帮助。

 var num = 3; if (!isNaN(num)) document.getElementById("isNum").innerHTML = '<span>'+num+"</span> is a number"; 
 #isNum span{ font-weight: bold; color: green; } 
 <div> <p id="isNum"></p> </div> 

Using <em> to emphasize the input would be more appropriate in this case. 在这种情况下,使用<em> 强调输入会更合适。

 <p id="isNum"><em>3</em> is a Number</p> 

Here is a sample snippet to work with (hope it is self-explanatory). 这是一个可使用的示例代码片段(希望它是不言自明的)。

Idea is to wrap the value which we need to be emphasize in an inline element (for example, span or em ). 想法是将需要强调的值包装 inline元素中(例如spanem )。

Note that <b> or i or <em> elements could also be used to get the results. 请注意, <b>i<em>元素也可以用于获取结果。

 function checknum() { // Get the `output` element var outputElement = document.querySelector("#isNum"); // Get the `input` value var num = document.querySelector("#numValue").value; // Validate: do we have a value and is it a number? var isNumber = num.length > 0 && !isNaN(num); // Format the HTML string to output // Wrap the value inside an inline element so that we could style it separately var outputHTML = "<em>" + num + "</em>"; // Output outputElement.innerHTML = isNumber ? outputHTML + " is a number" : "Not valid"; } 
 #isNum > em { color: blue; } #isNum > span { /* incase we go with span */ font-weight: bold; font-style: italic; color: blue; } 
 <div> <input type="text" id="numValue" /> <button onclick="checknum()">Insert anything</button> <p id="isNum"></p> </div> 

document.getElementById("isNum").innerHTML = "<span style='color: red;'>" + num +"</span> is a number";

Or (and this is generally considered a better practice) you could set a class on that span rather than an inline style. 或者(通常认为这是更好的做法),您可以在该范围内设置一个类,而不是内联样式。 Then define the color for that class in your CSS, wherever that lives for your page. 然后在您的页面上的任意位置定义CSS中该类的颜色。

JS: JS:

innerHTML = "<span class='number'>" + num +"</span> is a number";

CSS: CSS:

.number { color: red; }

Just add an element 只需添加一个元素

element.innerHTML = "<span class='blue'>" + num +"</span> is a number";

And style that class 和那个班级的风格

 function checknum() { var num = prompt('Insert anything'); var element = document.getElementById("isNum"); if (!isNaN(num)) element.innerHTML = "<span class='red'>" + num + "</span> is a number"; } 
 .red {color : red} 
 <div> <button onclick="checknum()">Insert anything : </button> <p id="isNum"></p> </div> 

Try this: 尝试这个:

   if (!isNaN(num))
    document.getElementById("isNum").innerHTML = '<span class="red">' + num +"</span> is a number";

You'll need to wrap the number with a DOM element 您需要使用DOM元素包装数字

Suppose this is the rendered HTML 假设这是呈现的HTML

<p id="isNum"><span class="num-color"> 1</span> is a color</p>

then add CSS 然后添加CSS

.num-color {
 color: yellow;
}

In order to get the HTML above, change your JavaScript code to this: 为了获取上面的HTML,请将您的JavaScript代码更改为此:

document.getElementById("isNum").innerHTML = '<span class="num-color">' + num + '</span>' +' is a number';

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

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