Change color of the text in contenteditable div

前端 未结 5 2033
陌清茗
陌清茗 2021-02-09 21:24

Demo is here

I have a contenteditable div. I want the functionality in the div as follows:

When I click on red anchor tag, the new text that I will write will be

5条回答
  •  故里飘歌
    2021-02-09 21:44

    This is what I did:

    $("#red").click(function () {
        $("div").html($("div").html() + "");
    });
    
    $("#blue").click(function () {
        $("div").html($("div").html() + "");
    });
    
    $("div").keypress(function (e) {
        e.preventDefault();
        $("div > span:last-child").text($("div > span:last-child").text() + String.fromCharCode(e.which));
    });
    

    Here is the JSFiddle

提交回复
热议问题