javascript - 附加 jQuery 的可调整大小的处理程序

标签 javascript jquery html css jquery-ui

查看此 jSFiddle .

我正在尝试附加处理程序以调整 $('oWrapper_'+num) 的大小。但它不会调整大小。原因是因为 $('#oWrapper_'+num) 在执行时还没有添加到 dom,所以选择器不会返回任何元素。

如何调整 $('oWrapper_'+num) 的大小而不是调整 cloudWrap 的大小?

num++
    var cloudWrap = $('<div />', { id: 'cloudWrap_'+num}),
        outerWrap = $('<div />').appendTo(cloudWrap)

        outerWrap.append(
            $('<div />', { class: 'tf', id: 'oWrapper_'+num, style: 'white-space:pre-line; font-size: 2vw;' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-ne hndl' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-se hndl' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-sw hndl' }),
            $('<div />', { class: 'ui-resizable-handle ui-resizable-nw hndl' })
        );


        jQuery('#oWrapper_'+num).resizable({
          handles: {
            'ne': '.ui-resizable-ne',
            'se': '.ui-resizable-se',
            'sw': '.ui-resizable-sw',
            'nw': '.ui-resizable-nw'
          },
          aspectRatio: 16 / 9,
        });

最佳答案

请看这个fiddle看看这是否能解决您的问题。

var num = 0;

num++;
var cloudWrap = $('<div />', { id: 'cloudWrap_'+num}),
    outerWrap = $('<div />').appendTo(cloudWrap);

outerWrap.append(
    $('<div />', { class: 'tf', id: 'oWrapper_'+num, style: 'white-space:pre-line; font-size: 2vw;' })
);

cloudWrap.appendTo('body');

$("#oWrapper_" + num).resizable({
    handles: 'ne, se, sw, nw',
    aspectRatio: true,
});

您的自定义 css 类干扰了调用 .resizable() 时添加的 jQuery-UI 类。

此外,我将 .resizable() 调用移到了代码块的末尾,以确保该元素在调用之前已存在于 DOM 中。

如果这不是您想要的,或者我误解了什么,请告诉我,我会进行必要的更改。谢谢!

关于javascript - 附加 jQuery 的可调整大小的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28833013/

相关文章:

javascript - 试图消除延迟

javascript - RESTful ajax - "item not found"的 HTTP 状态代码是什么?

javascript - setInterval 只运行一次

javascript - HTML5 : Reorder the children li of ul by drag and drop, li 元素之间的边框丢失

javascript - 不触发 HTML5 验证

javascript - 将 self 视为 npm 的节点模块

javascript - 使用 CORS 保护 API

javascript - 遍历对象数组以提取特定数据

javascript - 联系表格 7 : display/hide div on selection

html - 在悬停时显示另一个 div 内的 div