简体   繁体   English

Click事件在contenteditable div中不起作用

[英]Click event is not working in contenteditable div

i am going to place a delete button in the div so that user click the delete button then div will be deleted . 我将在div中放置一个删除按钮,以便用户单击删除按钮,然后div将被删除。 But currently there were 2 problem . 但是目前有两个问题。

(1)The button i placed is not properly aligned with the text in the div (1)我放置的按钮与div中的文本未正确对齐

(2)The button click event is not working . (2)按钮单击事件不起作用。

Please see my html 请看我的HTML

 $("#slider").on("change",function(){ var v=$(this).val(); $('.text.active').css('font-size', v + 'px'); }); $('.text').on('focus',function(){ $('.close-icon').addClass('active'); $('.text').removeClass('active'); $(this).addClass('active'); }); $(".close-icon.active").on("click",function(){ alert('hiii'); }); 
 .close-icon { border:1px solid transparent; background-color: transparent; display: inline-block; vertical-align: middle; outline: 0; cursor: pointer; } .close-icon:after { content: "X"; display: block; width: 15px; height: 15px; position: absolute; background-color: #FA9595; z-index:1; right: 35px; top: 0; bottom: 0; margin: auto; padding: 2px; border-radius: 50%; text-align: center; color: white; font-weight: normal; font-size: 12px; box-shadow: 0 0 2px #E50F0F; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="range" min="12" max="120" id="slider" /> <div class="text" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton" type="reset"></div> <div class="text text1" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton1" type="reset"></div> <div class="text text2" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton2" type="reset"></div> <div class="text text3" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton3" type="reset"></div> 

Please help to solve this . 请帮助解决此问题。

Your code is almost right just need few updates. 您的代码几乎正确,只需要很少的更新。

CSS: CSS:

Change position with float float更改position

/*position: absolute;*/
float:right;

Script: 脚本:

Your element is dynamic and that's why the event is not binding. 您的元素是动态的,这就是事件未绑定的原因。 Try following. 请尝试以下。

$(document).on("click",".close-icon",function(){

$(this).closest('div').remove();
//alert('hiii');

});

Here is working Fiddle 这是工作提琴

If have you modify any div after page load. 页面加载后是否修改了div。 Those changes are not registered in DOM. 这些更改未在DOM中注册。 So you need to target unchanged element as parent. 因此,您需要将未更改的元素作为父对象。 Better thing is you can target document or body tag. 更好的是您可以定位文档或正文标签。

<script>
$(document).on("click",".close-icon",function(){
alert('hiii');    
});

$("body").on("click",".close-icon",function(){
alert('hiii');    
});
</script>

See below code snippet final working code. 请参见下面的代码片段最终工作代码。

 $("#slider").on("change",function(){ var v=$(this).val(); $('.text.active').css('font-size', v + 'px'); }); $('.text').on('focus',function(){ $('.close-icon').addClass('active'); $('.text').removeClass('active'); $(this).addClass('active'); }); $(document).on("click",".close-icon.active",function(){ $(this).parent("div").remove(); }); 
 .close-icon { border:1px solid transparent; background-color: transparent; display: inline-block; vertical-align: middle; outline: 0; cursor: pointer; } .close-icon:after { content: "X"; display: block; width: 15px; height: 15px; position: relative; background-color: #FA9595; z-index:1; left: 100%; top: 0; bottom: 0; margin: auto; padding: 2px; border-radius: 50%; text-align: center; color: white; font-weight: normal; font-size: 12px; box-shadow: 0 0 2px #E50F0F; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="range" min="12" max="120" id="slider" /> <div class="text" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton" type="reset"></div> <div class="text text1" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton1" type="reset"></div> <div class="text text2" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton2" type="reset"></div> <div class="text text3" contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton3" type="reset"></div> 

If i get you right, here is the right code for your task 如果我说对了,这是适合您任务的代码

<input type="range" min="12" max="120" id="slider" />
    <div class="text"  contenteditable="true"  style="cursor:grab;">hello<button class="close-icon dbutton" type="reset"></button></div>
    <div class="text text1"  contenteditable="true" style="cursor:grab;">hello<button class="close-icon dbutton1" type="reset"></button></div>
    <div class="text text2"  contenteditable="true"  style="cursor:grab;">hello<button class="close-icon dbutton2" type="reset"></button></div>
    <div class="text text3"  contenteditable="true"  style="cursor:grab;">hello<button class="close-icon dbutton3" type="reset"></button></div>

No need for absolute position of icon 无需图标的绝对位置

.close-icon {
        border:1px solid transparent;
        background-color: transparent;
        display: inline-block;
        vertical-align: middle;
      outline: 0;
      cursor: pointer;
      position: relative;
    }
    .close-icon:before {
        content: "X";
        display: block;
        width: 15px;
        height: 15px;
        background-color: #FA9595;
        margin: auto;
        padding: 2px;
        border-radius: 50%;
        text-align: center;
        color: white;
        font-weight: normal;
        font-size: 12px;
        box-shadow: 0 0 2px #E50F0F;
        cursor: pointer;
    }

For me "click" worked well - I just put final code into function 对我来说,“点击”效果很好-我只是将最终代码放入函数中

$("#slider").on("change",function(){
       var v=$(this).val();
       $('.text.active').css('font-size', v + 'px');
    });

    $('.text').on('focus',function(){
        $('.text').removeClass('active');
        $(this).addClass('active');
    });

    $(".close-icon").on("click",function(){
        $(this).parent().remove();
    });

Check how it works here: https://jsfiddle.net/6ek8c0eq/ 在这里检查其工作方式: https//jsfiddle.net/6ek8c0eq/

Position absolute element should be wrapped by an position relative element else it will absolute to the window's position. 位置absolute元素应由位置relative元素包装,否则它将absolute位于窗口的位置。

Use should remove the parent of the button to remove the entire node.So, use parent and remove 使用应删除按钮的父项以删除整个节点。因此,请使用parentremove

 $("#slider").on("change", function() { var v = $(this).val(); $('.text.active').css('font-size', v + 'px'); }); $('.text').on('focus', function() { $('.text').removeClass('active'); $(this).addClass('active'); }); $(".close-icon").on("click", function() { $(this).parent().remove() }); 
 .close-icon { border: 1px solid transparent; background-color: transparent; display: inline-block; vertical-align: middle; outline: 0; cursor: pointer; } .text{ position: relative; margin: 20px 0; } .close-icon:after { content: "X"; display: block; width: 15px; height: 15px; position: absolute; background-color: #FA9595; z-index: 1; right: 35px; top: 0; bottom: 0; margin: auto; padding: 2px; border-radius: 50%; text-align: center; color: white; font-weight: normal; font-size: 12px; box-shadow: 0 0 2px #E50F0F; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="range" min="12" max="120" id="slider" /> <div class="text" contenteditable="true" style="cursor:grab;">hello <button class="close-icon dbutton" type="reset"> </div> <div class="text text1" contenteditable="true" style="cursor:grab;">hello <button class="close-icon dbutton1" type="reset"> </div> <div class="text text2" contenteditable="true" style="cursor:grab;">hello <button class="close-icon dbutton2" type="reset"> </div> <div class="text text3" contenteditable="true" style="cursor:grab;">hello <button class="close-icon dbutton3" type="reset"> </div> 

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

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