Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过onclick事件使文本加粗_Javascript_Jquery - Fatal编程技术网

Javascript 通过onclick事件使文本加粗

Javascript 通过onclick事件使文本加粗,javascript,jquery,Javascript,Jquery,我想通过单击按钮将文本加粗。请参阅下面的代码,我遇到的问题是无法将textareaid传递给boldTF()函数 <button type="button" id="tfButton">Add text</button> <div id="InputsWrapper"></div> <script> $(document).ready(function() { var tfCont = 0; var InputsWrap

我想通过单击按钮将文本加粗。请参阅下面的代码,我遇到的问题是无法将textareaid传递给
boldTF()
函数

<button type="button" id="tfButton">Add text</button>
<div id="InputsWrapper"></div>
<script>
$(document).ready(function() {
    var tfCont = 0;
    var InputsWrapper = $("#InputsWrapper");
    var x = InputsWrapper.length; 
    var namefield = $("#tfButton");

    $(namefield).click(function() {
        tfCont++;
        $(InputsWrapper).append('<div>' + '<div class="name" id="InputsWrapper_0' + tfCont + '">' + '<input type="textarea" id="field_' + tfCont + '" class="fTypex" placeholder="Type Here"/>' + '<br /><button type="button" onclick="boldTF("field_' + tfCont + '")">Bold</button>' + '<br>' + '</div>' + '</div>');
        x++;
        return false;
    });

    function boldTF(tmp){
        document.getElementById(tmp).style.fontWeight = "bold";
        //alert(tmp);
    };
});
</script>
添加文本
$(文档).ready(函数(){
var-tfCont=0;
var InputsRapper=$(“#InputsRapper”);
var x=输入振打器长度;
变量名称字段=$(“#tfButton”);
$(名称字段)。单击(函数(){
tfCont++;
$(InputsRapper).append(“”+“”+“”+”
粗体“+”
“+“”+”); x++; 返回false; }); 功能组(tmp){ document.getElementById(tmp.style.fontwweight=“bold”; //警报(tmp); }; });

谢谢。

删除内联事件处理程序并使用事件委派:

$(document).on('click', 'button', function () {
    $(this).closest('div.name').find('input').css('font-weight', 'bold')
})

许多问题


  • 能否共享HTML代码?不要为按钮编写内联事件处理程序。只需将一个委托事件处理程序附加到某个元素上(这里看起来应该是
    #InputsWrapper
    ),并让该处理程序自动处理所有事情。
    onclick=“boldTF”(“field”+tfCont+”)“
    将中断解析,因为双引号中有双引号。使用不引人注目的处理程序,就像Jon说的那样。@xDaevax:我已经通过包含html编辑了我的文章。
    $(function () {
        $("#tfButton").on("click", function(e) {
            e.preventDefault;
            var tfCont = InputsWrapper.length;
            $("#InputsWrapper").append(
                '<div class="name" id="InputsWrapper_0' + tfCont + '">' +
                '<input type="text" id="field_' + tfCont + '" class="fTypex" placeholder="Your first name"/>' +
                '<br /><button type="button" class="boldTF">Bold</button><br /></div>');
        });
        $("#InputsWrapper").on("click", ".boldTF", function(e) {
            e.preventDefault();
            $(this).parent().find(".fTypex").css({"font-weight":"bold"});
        });
    });