如何在可调整大小的事件后取消点击?

发布于 2024-11-02 00:46:56 字数 1493 浏览 0 评论 0原文

我正在利用 Jquery UI 可调整大小插件来调整各种 div 的大小。考虑以下 HTML 页面(嵌入了 JS):

<html>
<head></head>
<body>
<div id="site">
    <div>This element is resizable</div>
    <br />
    <div style="width: 200px;">This is another resizable element</div>
</div>

<!-- javascript includes -->
<script>
$(function() {
    $("#site div").click(function() {

        console.log("Clicked");    
        if($(this).hasClass("selected")) {
            $(this).removeClass("selected");
        }
        else {        // Currently not selected, so let's select it        
            $(this).addClass("selected").resizable({
                start: function(event, ui) {
                    console.log("Start");
                },
                resize: function(event, ui) {
                    console.log("Resize");
                },
                stop: function(event, ui) {
                    console.log("Stop");
                }
            });
        }
    });
});
</script>
</body>
</html>

因此,我试图在这个示例代码中完成的任务非常简单。当用户单击嵌套在“#site”中的 div 时,我希望该 div 被“选中”。因此,在此示例中,当触发 div 的单击事件时,名为“selected”的 CSS 类将添加到单击的 div 中。除了单击 div 之外,用户还可以调整 div 的大小。当用户调整所选 div 的大小时就会出现问题。显然,当用户首次选择 div 时会触发 click 事件,但当用户单击并拖动 div 边缘以调整其大小时也会触发 click 事件。单击事件将调用单击例程并取消选择 div。这不是我想要发生的事情。仅当所有可调整大小的事件触发后,才会触发单击事件。

有了这个,我的问题是,如何处理这种情况,以允许用户单击并选择 div,同时允许调整 div 大小,但在拖动 div 并调整大小时不取消选择?感谢您的帮助。提前致谢。

I am utilizing the Jquery UI resizable plugin in order to resize various div's. Consider the following HTML page (with JS embedded):

<html>
<head></head>
<body>
<div id="site">
    <div>This element is resizable</div>
    <br />
    <div style="width: 200px;">This is another resizable element</div>
</div>

<!-- javascript includes -->
<script>
$(function() {
    $("#site div").click(function() {

        console.log("Clicked");    
        if($(this).hasClass("selected")) {
            $(this).removeClass("selected");
        }
        else {        // Currently not selected, so let's select it        
            $(this).addClass("selected").resizable({
                start: function(event, ui) {
                    console.log("Start");
                },
                resize: function(event, ui) {
                    console.log("Resize");
                },
                stop: function(event, ui) {
                    console.log("Stop");
                }
            });
        }
    });
});
</script>
</body>
</html>

So, what I'm trying to accomplish in this sample code is pretty simple. When a user clicks on a div nested within "#site", I want the div to become "selected". So, in this example, when the click event for the div is fired, a CSS class called "selected" will be added to the clicked div. In addition to clicking the div, the user can resize the div. The problem arises when the user resizes the selected div. Obviously, a click event fires when the user first selects a div, but a click event also fires when the user clicks and drags the edges of the div to resize it. The click event will call the click routine and de-select the div. This is not what I want to happen. The click event fires only after all the resizable events have fired.

With that, my question is, how could I handle this scenario to allow the user to click and select the div, but also to allow the div to be resized, but not de-selected when the div is dragged and resized? Your help is appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-11-09 00:46:56

好问题。可调整大小正在利用事件冒泡。实际的调整大小手柄是可调整大小的内部元素。因此,检查事件目标的类将为您提供所需的内容。

http://jsfiddle.net/bmvDs/1/

$('#test').click(function(e){
    if(!$(e.target).is('.ui-resizable-handle')) // not if it is the handle
        $(this).toggleClass('selected');
}).resizable();

Good question. Resizable is utilizing event bubbling. The actual resize handles are inner elements of your resizable. So, checking the class of the event target will give you what you need.

http://jsfiddle.net/bmvDs/1/

$('#test').click(function(e){
    if(!$(e.target).is('.ui-resizable-handle')) // not if it is the handle
        $(this).toggleClass('selected');
}).resizable();
葬心 2024-11-09 00:46:56

一种选择可能是从一开始就将所有 div 设置为可调整大小,并在单击时处理样式:

$(function() {
    var resizables = $('#site div');
    resizables.resizable({

               start: function(event, ui) {
                    console.log("Start");

                    // Remove the selected class from all other divs first
                    resizables.each(function(){ $(this).removeClass('selected'); });

                    // Set this div as selected
                    ui.element.addClass('selected');
                    // Carry on doing whatever else you want to do

               },
               resize: function(event, ui) {
                    console.log("Resize");
               },
               stop: function(event, ui) {
                    console.log("Stop");
               }

    });
});

One option might be to set all your divs as resizable from the outset and handle the styles on click:

$(function() {
    var resizables = $('#site div');
    resizables.resizable({

               start: function(event, ui) {
                    console.log("Start");

                    // Remove the selected class from all other divs first
                    resizables.each(function(){ $(this).removeClass('selected'); });

                    // Set this div as selected
                    ui.element.addClass('selected');
                    // Carry on doing whatever else you want to do

               },
               resize: function(event, ui) {
                    console.log("Resize");
               },
               stop: function(event, ui) {
                    console.log("Stop");
               }

    });
});
请爱~陌生人 2024-11-09 00:46:56

jQuery .one 是你的朋友!这将允许单击每个具有 .foo 类的元素。

$(".foo").one("click", function() {
  alert("This will be displayed only once.");
});

jQuery .one is your friend! This will allow a single click on each element with class .foo.

$(".foo").one("click", function() {
  alert("This will be displayed only once.");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文