问题
I have an html div element in my application as follows
<div id="DivId" contenteditable="true" style="border:1px solid black"></div>
I need to limit the number of character that can be typed in the div. Like giving maxlength attribute to textbox how can I do this? Its very helpful if I am able to do it without using javascript or jquery.
回答1:
There are some CSS attributes like max-height
or max-width
which do limit the width for block level elements.
So you can code this out like this :
<div style='max-width:100px'>Content</div>
<span style='max-width:100px; display:block;'>Content</span>
But be careful, as they do not properly support IE6 : Refer this URL .
But if you just mean length of content,I am sorry to say that you can't do that in HTML. You need to use a piece of javascript to do this. Take a look at JQuery, it's really easy to do.
来源:https://stackoverflow.com/questions/21876085/max-length-property-for-an-editable-div