简体   繁体   English

同一条javascript行中的两个不同的CSS类

[英]Two different css classes in the same javascript line

I'm trying to assign these css values (below) for the javascript line in the example below, but don't know a way to target valueB with the . 我正在尝试在下面的示例中为javascript行分配这些css值(如下),但不知道使用来定位valueB的方法。 valueB-class . valueB级

$(".valueA").html(valueA + " valueB" + ((valueA > 1) ? 's': ''));

.valueA-class { font-size:X }
.valueB-class { font-size:XX }

Here is an example of what I need help with (you may have to click on the input boxes in the results panel to get the calculations to show up - that's what I had to do): http://jsfiddle.net/hughett/g21g8t85/ 这是我需要帮助的示例(您可能必须在结果面板中单击输入框才能显示计算结果-这就是我要做的事情): http : //jsfiddle.net/hughett/ g21g8t85 /

Welcome to stackoverflow! 欢迎来到stackoverflow!

Your question seems a bit vague. 您的问题似乎有点含糊。 I assume that this is you want to achieve. 我假设这是您想要实现的。 In the specific example the value of the class is changed through the use of the jquery attr function. 在特定示例中,通过使用jquery attr函数来更改类的值。 Firstly, the specific div in which our text is placed is retrieved and then the value gets specified. 首先,检索放置文本的特定div,然后指定值。 I am attaching a code snippet below. 我在下面附上代码片段。

A general note, using a . 使用的一般注释。 in css indicates that you are referring to a class so there is no need to attach a -class in the name. css中的in表示您正在引用一个类,因此无需在名称中附加-class。

 $( "#myButton" ).on( "click", function() { var attr = $("#myText").attr('class'); console.log(attr); if (attr == "valueA") { $("#myText").attr("class","valueB"); } else { $("#myText").attr("class","valueA"); } }); 
 .valueA { font-size:11pt } .valueB { font-size:25pt } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="myButton" type="button">Change Text size</button> <div id="myText" class="valueA">sdsa asd aasdaas asdjlasj dasdkas asldjsalj slad TEST</div> 

EDIT to include another answer 编辑以包括其他答案

In order for the text included in a single span to have different font-size you need to separate it somehow. 为了使单个跨度中包含的文本具有不同的字体大小,您需要以某种方式将其分开。 In the specific example, I have added a second span in the respective div and adjusted the cacl_summary method to get the expected result. 在特定示例中,我在相应的div中添加了第二个跨度,并调整了cacl_summary方法以获得预期的结果。

The code is available below; 该代码可在下面找到; I have also updated the jsfiddle here 我也在这里更新了jsfiddle

<div style="background:yellow;"><span class="label">Simple payback</span>
<span class="figure sp"></span> <span class="figure year"></span></div>

function calc_summary(){
    if (cspy) {
       sp = parseFloat($("input[name=upgrade]").val()) / cspy;
       if (sp) {
          sp = (sp < 100) ? sp.toString().substring(0, 4) : sp;
          $(".sp").html(sp);
          $(".year").html(" years" + ((sp > 1) ? 's': ''));
          $(".ror").html(parseInt((1/sp) * 100) + '%');
       }
    }
 }

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

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