jquery - 如果在运行时创建,可调整大小的句柄将不起作用

标签 jquery jquery-ui-resizable

尝试实现通过外部元素调整内部元素大小的简单功能。

如果通过 js 创建元素和自定义句柄,JQuery 可调整大小的句柄将不起作用。下面是js代码。

var resizable_handles_html = '[div sructure]'; //editor ommiting codes :)

$(function(){
  $("#clickMe").click(function(){
    $("[tried to put div here]")   //editor ommiting codes :)
      .css({
        'background':'white',
        'width':'100px',
        'height':'100px',
        'border':'dotted 1px #000',
        'position':'relative'
      })
      .append(resizable_handles_html)
      .draggable()
      //.resizable({handles: "n, e, s, w, ne, se, sw, nw"})
      .resizable({
        handles: {
          'nw': '.nwgrip',
          'ne': '.negrip',
          'sw': '.swgrip',
          'se': '.segrip',
          'n': '.ngrip',
          'e': '.egrip',
          's': '.sgrip',
          'w': '.wgrip'
        }
      })
      .append(
        $("[tried to put div here]")   //editor ommiting codes :)
          .attr({
            id:'text',
            contenteditable:'true'
          })
          .css({
            'background':'blue',
            'width':'auto',
            'height':'auto',
            'border':'dotted 1px #000',
            '-moz-border-radius': '15px',
            '-webkit-border-radius': '15px',
            'border-radius': '15px',
            '-khtml-border-radius': '15px'
          })
      )
      .appendTo('body');
  });
})

尝试粘贴变量“ressized_handles_html”的内容,但仅显示引号。

现在正在创建元素,但抛出错误

"Uncaught TypeError: Cannot read property 'ownerDocument' of undefined" on click of that element."

对于可拖动来说,这肯定不会发生。创建了一个 fiddle http://jsfiddle.net/keshabdey/7m8GF/5/

任何帮助将不胜感激。

最佳答案

更新了 fiddle ,似乎可以工作:

修复了标记,句柄具有多个类属性

var resizable_handles_html = '<div class="ui-resizable-handle ui-resizable-nw nwgrip">    </div>'+
'<div class="ui-resizable-handle ui-resizable-ne negrip"></div>'+
'<div class="ui-resizable-handle ui-resizable-sw swgrip"></div>'+
'<div class="ui-resizable-handle ui-resizable-se segrip"></div>'+
'<div class="ui-resizable-handle ui-resizable-n ngrip"></div>'+
'<div class="ui-resizable-handle ui-resizable-s sgrip"></div>'
'<div class="ui-resizable-handle ui-resizable-e egrip"></div>'+
'<div class="ui-resizable-handle ui-resizable-w wgrip"></div>';

也使用默认的.res定大小构造函数

http://jsfiddle.net/7m8GF/8/

关于jquery - 如果在运行时创建,可调整大小的句柄将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20597183/

相关文章:

javascript - 在 AngularJS 中将每个 HTML 元素附加到 DOM 之前对其进行预处理

javascript - 我如何知道一次切换了多少个 Waypoints 实例?

asp.net - jQuery 验证插件和 ASP.NET 验证器 Web 表单

javascript - 如果某个字段具有值 jQuery,则从多个表单中序列化某个表单的表单数据

javascript - float 图调整大小错误,高度无效

javascript - GM_registerMenuCommand 未定义

javascript - 调整大小时,Jquery Resizable Div 宽度和高度变为 0

javascript - jQuery UI - 动态设置可调整大小元素的 minHeight

jQuery UI 可调整大小也可反向调整大小

jquery - 如何知道 jquery resizing 中正在使用哪个句柄