简体   繁体   English

在可编辑div中使用jquery click事件

[英]Using jquery click event inside an editable div

I use JQuery to register events for different tags. 我使用JQuery注册不同标签的事件。 Here is the code so far: 这是到目前为止的代码:

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-1.7.js"></script>
    </head>
    <body>
        <p>First Paragraph</p>

        <p>Second Paragraph</p>
        <p>Yet one more Paragraph</p>

        <div><p>another one</p></div>

        <p><strong>big one</strong></p>

        <div contentEditable="true"><p>hello <b>there</b></p></div>

        <script>
            $("p").click(function () { 
                         alert('p tag'); 
                         });

            $("div").click(function () { 
                         alert('div tag'); 
                         });

            $("b").click(function () { 
                         alert('strong tag'); 
                         });

            </script>

    </body>
</html>

I want to develop a simple text editor and want to capture click events so that I can update the state of my buttons (bold, italics, ...). 我想开发一个简单的文本编辑器,并希望捕获单击事件,以便可以更新按钮的状态(粗体,斜体等)。 I want the click events to work inside an editable div as well but they only fire once when the div tag is selected and afterwards when the editing begins, clicking anywhere inside that div doesn't fire, even though I have p and b tags inside the div. 我希望click事件也能在可编辑的div内运行,但是当div标签被选中时它们只会触发一次,然后在编辑开始时触发,即使我内部有p和b标记,在该div内的任何位置单击也不会触发div。 Is is possible to achieve this using an editable div? 是否可以使用可编辑的div实现此目的? Basically I want to be able to know if the cursor is inside ab tag or an i tag so that I can update the state of my buttons. 基本上,我希望能够知道光标是在ab标签还是i标签内,以便我可以更新按钮的状态。

你可以使用.one

$("editableDIV").one("click",function(){...});

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

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