简体   繁体   English

使用nicedit编辑后查找内容的高度和宽度

[英]finding height and width of content after editing with nicedit

I have a div that is resizable and draggable via jquery. 我有一个可调整大小和可通过jQuery拖动的div。

It is also editable with niceedit. 也可以使用niceedit进行编辑。

The problem I have is when i add more text to the div whilst in editmode the div does not expand to fit the text in and the text spills out of the div. 我的问题是,当我在div中添加更多文本时,在edit模式下div不能扩展为适合文本,并且文本会溢出div。

Does anyone have any idea how I can update the height and width of the div from the updated contents? 有谁知道如何从更新后的内容中更新div的高度和宽度?

You can see on this example if you start adding text the box does not expand: 您可以在此示例上看到,如果您开始添加文本,则该框不会展开:

http://jsfiddle.net/j6FLa/20/ http://jsfiddle.net/j6FLa/20/

Code to match jsfiddle 匹配jsfiddle的代码

.dragbox {
position:absolute;
width:10px;
height:25px;
padding: 0.0em;
margin:25px;
cursor:move;
z-index:2
}

#draggable {
min-width:150px;
min-height:150px;
height:auto;
width:auto;
border:1px solid #ff0000;
padding:25px;
}



//<![CDATA[
bkLib.onDomLoaded(function () {
    var myNicEditor = new nicEditor();
    myNicEditor.setPanel('myNicPanel');
    myNicEditor.addInstance('draggable');
});
//]]>


$("div.dragbox").dblclick(function (e) {
    $('.dragbox').css('cursor', 'select');
    $(this).draggable('disable').removeClass('ui-state-disabled').focus();
    }).on('blur', function () {
    $(this).draggable('enable');
});

$(function () {
    $("#draggable").draggable().resizable();
});


<div id="myNicPanel" style="width: 525px;"></div>
<div id="draggable" class="dragbox" contenteditable="true" style="">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed magna dolor</div>

I'm still a noob at programming, so there might be much better solutions. 我仍然是编程的新手,因此可能会有更好的解决方案。 But I can show you what I have done with a similar problem. 但我可以向您展示我针对类似问题所做的工作。 Not sure if that is what you need though. 不确定是否是您所需要的。

function getHeight(id)
{
    if(document.getElementById(id) == null)
        return Number(0);
    else
    return document.getElementById(id).offsetHeight; // Change to .offsetWidth to gain width :)
}

You create and call this function by sending the ID og the element that you want to get the width/height. 通过发送ID og要获取宽度/高度的元素来创建并调用此函数。 The function is made so it will first check if the ID is present, if not it returns 0. If it is, it returns the inner width/height. 完成此功能后,它将首先检查ID是否存在,如果不存在,则返回0。如果存在,则返回内部宽度/高度。 If you are 100% sure that the ID you are asking for is present you only need the last line of the function. 如果您100%确定要输入的ID存在,则只需要函数的最后一行。

You could use it like so: 您可以这样使用它:

var h = getHeight("draggable");

This will return the height of the draggable div and store it in the variable h. 这将返回可拖动div的高度并将其存储在变量h中。

Hope it is useful :) 希望它有用:)

use the stop event for the resizable widget to get the height - the problem you have is the text is not wrapped in another element if you wrap the text in another element you can target that element if not here's sort of a hackey solution also you can use this for the css 将stop事件用于可调整大小的窗口小部件以获取高度-您遇到的问题是,如果您将文本包装在另一个元素中,则文本不会被包装在另一个元素中,如果没有,则可以定位该元素,这也是一种hackey解决方案将此用于CSS

#draggable {word-wrap:break-word;}

use something like this for the js 对js使用类似这样的东西

$("#draggable").draggable().resizable({stop: function( event, ui ) {
        var tempHeight = $(this).css({"height" : "auto"}).height();
        $(this).css({"height" : "0px"}).animate({
        height : tempHeight
        },1000);
        }
     });

http://jsfiddle.net/j6FLa/26/

UPDATE I have updated my fiddle to add an extra p element in there and set it to the minimum height of the p you might like this way better 更新我已经更新了小提琴,在其中添加了一个额外的p元素,并将其设置为您可能更喜欢这种方式的p的最小高度

http://jsfiddle.net/j6FLa/28/ http://jsfiddle.net/j6FLa/28/

the html HTML

<div id="myNicPanel" style="width: 525px;"></div>
<div id="draggable" class="dragbox" contenteditable="true" style=""><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed magna dolor asdfa asdfasdf asdf</p></div>

the js js

$(function () {
    $("#draggable").draggable().resizable({stop: function( event, ui ) {
        var pHeight = $(this).children('p').height();
        $(this).css('min-height', pHeight);
        }
     });
    $("#draggable").on( "dragstop", function( event, ui ) {alert('yes');} );
});

My previous answer can only take you as far as FINDING the height/width, but it does actually not CHANGE the height/width of the div itself. 我之前的回答只能带您找到高度/宽度,但实际上并不能改变div本身的高度/宽度。

So after the width/height is found using my previous answer, here is how to change the width/height of the div in question. 因此,在使用我之前的答案找到宽度/高度之后,这是如何更改所讨论的div的宽度/高度。

Lets say your div ID is "draggable" and your p ID is "text". 假设您的div ID是“可拖动的”,而p ID是“文本”。 When you are done editing your text you can update width and height by calling a function (defined below). 编辑完文本后,您可以通过调用函数(定义如下)来更新宽度和高度。

resize("draggable", "text"); 

You can imitate CSS styles in javascript like so: 您可以像这样在javascript中模仿CSS样式:

function resize(id1, id2)
{
    var divName = document.getElementById(id1);
    divName.style.position = absolute;
    divName.style.width = getWidth(id2)+"px"; // remember the +"px"!!
    divName.style.height = getHeight(id2)+"px";
}

This will set the height and width of your div to be equal to the height and width of your text area. 这会将div的高度和宽度设置为等于文本区域的高度和宽度。 By doing like so you can change the height and width of any giving ID according to any other in your project. 这样,您可以根据项目中的任何其他ID更改任何给定ID的高度和宽度。 getWidth() and getHeight() is a reference to my previous post. getWidth()和getHeight()是对我以前的文章的引用。

A combination of my 2 answers should be able to accomplish what you are looking for. 我的2个答案的组合应该能够完成您想要的。 I apologize for not giving a proper answer the first time around, but I'm still new both to programming and to this site. 对于第一次没有给出正确的答案,我深表歉意,但是对于编程和本网站,我仍然是新手。 I'll try to do better in future. 将来我会尝试做得更好。 I hope that this time around you'll succeed in both getting and setting the sizes of your div. 我希望这次您能成功获取并设置div的大小。

--EDIT-- - 编辑 -

code from my previous post: 我以前的帖子中的代码:

function getHeight(id)
{
    if(document.getElementById(id) == null) //simply a guard that returns 0 if a wrong ID is stated. In reality the return statement is enough.
        return Number(0);
    else
    return document.getElementById(id).offsetHeight; // Change to .offsetWidth to gain width :)
}

function getWidth(id)
{
    return document.getElementById(id).offsetWidth;
}

-- END EDIT-- -结束编辑-

  • Molle 莫尔

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

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