मैं संपादन के लिए उपयोगकर्ता के लिए सामग्री के साथ एक textarea भर रहा हूं।
क्या सीएसएस के साथ सामग्री को फिट करना संभव है (जैसे overflow:show
डिव के लिए)?
मैं संपादन के लिए उपयोगकर्ता के लिए सामग्री के साथ एक textarea भर रहा हूं।
क्या सीएसएस के साथ सामग्री को फिट करना संभव है (जैसे overflow:show
डिव के लिए)?
जवाबों:
ज़रुरी नहीं। यह सामान्य रूप से जावास्क्रिप्ट का उपयोग करके किया जाता है।
यहाँ ऐसा करने के तरीकों की एक अच्छी चर्चा है ...
केवल एक लाइन
<textarea name="text" oninput='this.style.height = "";this.style.height = this.scrollHeight + "px"'></textarea>
this.scrollHeight + "px"
से this.scrollHeight + 3 + "px"
स्क्रॉलबार नहीं दिखा।
यहां एक फ़ंक्शन है जो jQuery के साथ काम करता है (केवल ऊंचाई के लिए, चौड़ाई नहीं):
function setHeight(jq_in){
jq_in.each(function(index, elem){
// This line will work with pure Javascript (taken from NicB's answer):
elem.style.height = elem.scrollHeight+'px';
});
}
setHeight($('<put selector here>'));
नोट: ऑप ने एक समाधान के लिए कहा जो जावास्क्रिप्ट का उपयोग नहीं करता है, हालांकि यह कई लोगों के लिए उपयोगी होना चाहिए जो इस प्रश्न के पार आते हैं।
यह एक बहुत ही सरल उपाय है, लेकिन यह मेरे लिए काम करता है:
<!--TEXT-AREA-->
<textarea id="textBox1" name="content" TextMode="MultiLine" onkeyup="setHeight('textBox1');" onkeydown="setHeight('textBox1');">Hello World</textarea>
<!--JAVASCRIPT-->
<script type="text/javascript">
function setHeight(fieldId){
document.getElementById(fieldId).style.height = document.getElementById(fieldId).scrollHeight+'px';
}
setHeight('textBox1');
</script>
<style> textarea { resize: none; overflow: auto; } </style> <!--TEXT-AREA--> <textarea onkeyup="setHeight.call(this);">Hello World</textarea> <!--JAVASCRIPT--> <script type="text/javascript"> function setHeight(){ this.style.height = '1px'; this.style.height = this.scrollHeight + 'px'; } </script>
मूल रूप से आपको इसकी ऊंचाई 1px निर्धारित करनी होगी, इस कारण से: stackoverflow.com/questions/10722058/…
डायनेमिक टेक्सारिया नियंत्रण के लिए एक और सरल उपाय।
<!--JAVASCRIPT-->
<script type="text/javascript">
$('textarea').on('input', function () {
this.style.height = "";
this.style.height = this.scrollHeight + "px";
});
</script>