简体   繁体   English

复选框检查上的粗体文字

[英]Bold text on checkbox check

I have a div with an h3 tag who's text I would like to bold when a checkbox is checked. 我有一个带有h3标签的div,当我选中一个复选框时,我想要加粗文字。 I have seen solutions with labels but I don't believe it works in my case. 我见过带标签的解决方案,但我不相信它适用于我的情况。 Here's the code: 这是代码:

HTML: HTML:

<div class="imagecontainer">
  <div id="imagewrap" class="wrap">
    <img src="../images/travel.gif" id="img_prev" width="450" height="450" />
    <h3 class="desc">"Perfection is not attainable, but if we chase perfection we can catch excellence."</h3>
  </div>
</div>

<input id="boldCheckbox" type="checkbox" class="boldCheckbox"/>
<label id="label1">Bold</label>

To be clear, I would like the h3 text with class="desc" to be bold on check. 为了清楚起见,我希望带有class="desc"的h3文本在检查时加粗。 Thanks! 谢谢!

You need a change handler for the input: 您需要输入的更改处理程序:

document.getElementById("boldCheckbox").onchange = function() {
    var h3 = document.getElementsByClassName("desc")[0];
    if (this.checked) {
        h3.style.fontWeight = "bold";
    } else {
        h3.style.fontWeight = "normal";
    }
}

Demo: http://jsfiddle.net/jvk3qpvp/1/ 演示: http//jsfiddle.net/jvk3qpvp/1/

var btnBold = document.getElementById("boldCheckbox"),
    title   = document.querySelector("#imagewrap h3");

btnBold.addEventListener("change", function(){
   title.style.fontWeight = this.checked ? "bold" : "normal";
});

jsBin demo jsBin演示

If you're OK using jQuery... here's a quite simple example: 如果你可以使用jQuery ......这是一个非常简单的例子:

 $('#boldCheckbox').on('change', function() { $('#imagewrap h3').css({fontWeight: this.checked?'bold':'normal'}); }); 
 h3 { font-weight : normal} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="imagecontainer"> <div id="imagewrap" class="wrap"> <h3 class="desc">"Perfection is not attainable, but if we chase perfection we can catch excellence."</h3> </div> </div> <input id="boldCheckbox" type="checkbox" class="boldCheckbox" /> <label id="label1">Bold</label> 

Logically, if your h3 is already bold by default than simply inverse the logic: 从逻辑上讲,如果您的h3默认情况下已经是粗体而不是简单地反转逻辑:

this.checked? 'normal' : 'bold'

You can use JavaScript to look at the checkbox and if it's checked, make it bold (font weight 700) and not, don't make it bold. 您可以使用JavaScript查看复选框,如果选中它,请将其设为粗体(字体粗细700),不要使其变为粗体。 Keep in mind the below code will show the h3 bold on page load. 请记住,以下代码将在页面加载时显示h3粗体。 I'm not sure how you want to handle that. 我不确定你想怎么处理。

$('#boldCheckbox').on('change', function () {
    if ($('#boldCheckbox').is(":checked")) {
        $('h3').css('font-weight', '700');
    }
    else {
        $('h3').css('font-weight', '100');
    }
});

https://jsfiddle.net/kuwu6kog/ https://jsfiddle.net/kuwu6kog/

WORKING 100% 工作100%

Use this code to make H3 BOLDER 使用此代码制作H3 BOLDER

HTML HTML

<div class="imagecontainer">
          <div id="imagewrap" class="wrap">
              <img src="icons/frontstreet.png" id="img_prev" width="450" height="450" />
              <h3 class="desc">"Perfection is not attainable, but if we chase perfection we can catch excellence."</h3>
          </div>
      </div>

      <input id="boldCheckbox" onclick="makeH3Bold(this)" type="checkbox" class="boldCheckbox"/>

JAVASCRIPT: JAVASCRIPT:

function makeH3Bold(o){
if(o.checked){
    $('.desc').css('textShadow','#000000 1px 0px 0px');
}
else{
    $('.desc').css('textShadow','#FFFFFF 0px 0px 0px');
}}

You can add other class in your tag with bold style. 您可以在标记中添加粗体样式的其他类。 Try this: 尝试这个:

document.getElementById("boldCheckbox").onchange = function() {
    var imageWrap = document.getElementById("imagewrap");
    var h3 = imageWrap.getElementsByTagName("h3")[0];
    if (this.checked) {
        h3.className = "desc selectedH3";
    } else {
        h3.className = "desc";
    }
}

selectedH3 could be like: selectedH3可能是这样的:

.selectedH3 {
    font-weight: bold;
}

This add or remove the class selectedH3 from your h3 tag. 这会从h3标记中添加或删除selectedH3类。

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

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