2009-11-19 12 views
21

मेरे पास <textarea> तत्व है। क्या मैं जावास्क्रिप्ट का उपयोग यह पता लगाने के लिए कर सकता हूं कि (उदाहरण के लिए) इसमें टेक्स्ट की 10 पंक्तियां हैं?जावास्क्रिप्ट का उपयोग कर <textarea > में पंक्तियों की संख्या कैसे प्राप्त करें?

+0

आप अपने प्रश्न को स्पष्ट करने के लिए तो यह नहीं लगता है जैसे आप कर रहे हैं चाहते हो सकता है 'textarea' तत्व से' पंक्तियों = 'विशेषता को वापस करने का प्रयास कर रहा है। –

+2

आप ऐसा क्यों करना चाहते हैं? मुझे एक बुरा आवश्यकता विनिर्देश समझ में आता है। – Randell

उत्तर

0

आप जावास्क्रिप्ट डॉम के माध्यम से क्षेत्र तक पहुंच सकते हैं और बस न्यूलाइन अक्षरों की संख्या को गिन सकते हैं।

oArea = document.getElementById('myTextField'); 
var aNewlines = oArea.value.split("\n"); 
var iNewlineCount = aNewlines.length(); 
+3

यह केवल के लिए काम करेगा। यदि पाठ ऑटो-लपेटता है तो इसे करने का एकमात्र तरीका एक निश्चित आकार के फ़ॉन्ट का उपयोग करना है, सभी वर्णों की गणना करें और फिर उन्हें पंक्ति में अनुमत वर्णों से विभाजित करें। और यह मानते हुए कि प्रक्रिया में कोई नई लाइन नहीं है ... \ n कोई भी ?! – Frankie

+0

@ फ्रेंकी, ठीक है, लेकिन इसे सही तरीके से कैसे गणना करें? – Mask

+0

यदि आप एक विकल्प हैं जो नई लाइनों की संख्या = पंक्तियों की संख्या –

31

अच्छी तरह से मुझे ऐसा करने का एक आसान तरीका मिला, लेकिन आपको सीएसएस में टेक्स्टरेरा की रेखा-ऊंचाई सेट करने की आवश्यकता होगी। मैंने स्क्रिप्ट ta.style.lineHeight के अंदर लाइन ऊंचाई को पढ़ने की कोशिश की लेकिन यह एक मान वापस नहीं प्रतीत होता है।

सीएसएस

#ta { width: 300px; line-height: 20px; } 

एचटीएमएल

<textarea id="ta">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit, nisl eget dapibus condimentum, ipsum felis condimentum nisi, eget luctus est tortor vitae nunc. Nam ornare dictum augue, non bibendum sapien pulvinar ut. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Cras congue congue purus, quis imperdiet tellus ornare in. Nulla facilisi. Nulla elementum posuere odio ut ultricies. Nullam tempus tincidunt elit eget posuere. Pellentesque sit amet tellus sapien. Praesent sed iaculis turpis. Nam quis nibh diam, sed mattis orci. Nullam ornare adipiscing congue. In est orci, consectetur in feugiat non, consequat vitae dui. Mauris varius dui a dolor convallis iaculis.</textarea> 

स्क्रिप्ट

var taLineHeight = 20; // This should match the line-height in the CSS 
var taHeight = ta.scrollHeight; // Get the scroll height of the textarea 
ta.style.height = taHeight; // This line is optional, I included it so you can more easily count the lines in an expanded textarea 
var numberOfLines = Math.floor(taHeight/taLineHeight); 
alert("there are " + numberOfLines + " lines in the text area"); 

अद्यतन: धन्यवाद कीड़े बाहर काम करने के लिए @Pebbl करने के लिए, इस कोड को प्राप्त करने के लिए आवश्यक है पाठ सामग्री की ऊंचाई (demo)

var calculateContentHeight = function(ta, scanAmount) { 
    var origHeight = ta.style.height, 
     height = ta.offsetHeight, 
     scrollHeight = ta.scrollHeight, 
     overflow = ta.style.overflow; 
    /// only bother if the ta is bigger than content 
    if (height >= scrollHeight) { 
     /// check that our browser supports changing dimension 
     /// calculations mid-way through a function call... 
     ta.style.height = (height + scanAmount) + 'px'; 
     /// because the scrollbar can cause calculation problems 
     ta.style.overflow = 'hidden'; 
     /// by checking that scrollHeight has updated 
     if (scrollHeight < ta.scrollHeight) { 
      /// now try and scan the ta's height downwards 
      /// until scrollHeight becomes larger than height 
      while (ta.offsetHeight >= ta.scrollHeight) { 
       ta.style.height = (height -= scanAmount)+'px'; 
      } 
      /// be more specific to get the exact height 
      while (ta.offsetHeight < ta.scrollHeight) { 
       ta.style.height = (height++)+'px'; 
      } 
      /// reset the ta back to it's original height 
      ta.style.height = origHeight; 
      /// put the overflow back 
      ta.style.overflow = overflow; 
      return height; 
     } 
    } else { 
     return scrollHeight; 
    } 
} 

var calculateHeight = function() { 
    var ta = document.getElementById("ta"), 
     style = (window.getComputedStyle) ? 
      window.getComputedStyle(ta) : ta.currentStyle, 

     // This will get the line-height only if it is set in the css, 
     // otherwise it's "normal" 
     taLineHeight = parseInt(style.lineHeight, 10), 
     // Get the scroll height of the textarea 
     taHeight = calculateContentHeight(ta, taLineHeight), 
     // calculate the number of lines 
     numberOfLines = Math.ceil(taHeight/taLineHeight); 

    document.getElementById("lines").innerHTML = "there are " + 
     numberOfLines + " lines in the text area"; 
}; 

calculateHeight(); 
if (ta.addEventListener) { 
    ta.addEventListener("mouseup", calculateHeight, false); 
    ta.addEventListener("keyup", calculateHeight, false); 
} else if (ta.attachEvent) { // IE 
    ta.attachEvent("onmouseup", calculateHeight); 
    ta.attachEvent("onkeyup", calculateHeight); 
} 
+2

+1, यह अब तक का सबसे विश्वसनीय दृष्टिकोण है। एक नोट: सुनिश्चित करें कि textarea 'पंक्तियों = 1' के साथ शुरू होता है, या फिर इसकी' scrollHeight' कृत्रिम रूप से उच्च हो सकती है। –

+1

@Mottie '। स्टाइल' ऑब्जेक्ट केवल इनलाइन शैलियों को वापस कर देगा। गणना मूल्य प्राप्त करने के लिए आपको पुराने विंडो के लिए 'window.getComputedStyle' और 'Elm.currentStyle' पर फ़ॉलबैक का उपयोग करना चाहिए। उस +1 के अलावा :) – Pebbl

+1

इनपुट @pebbl के लिए धन्यवाद! मैंने उपरोक्त सुझावों को जोड़ दिया है और [डेमो] (http://jsfiddle.net/Mottie/PfD7L/) बनाया है। ध्यान दें कि जब आप आधुनिक ब्राउज़र में टेक्स्टरेरा का मैन्युअल रूप से आकार बदलते हैं, तो आकार की ऊंचाई से मेल खाने के लिए लाइनों की संख्या बदल जाएगी, न कि सामग्री। – Mottie

0
function countLines(area,maxlength) { 
     // var area = document.getElementById("texta") 
     // trim trailing return char if exists 
     var text = area.value.replace(/\s+$/g, "") 
     var split = text.split("\n") 
     if (split.length > maxlength) { 
      split = split.slice(0, maxlength); 
      area.value = split.join('\n'); 
      alert("You can not enter more than "+maxlength.toString()+" lines"); 
     } 
     return false; 
    } 

यह एक सरल है और परीक्षण एक

+3

एक नई लाइन निश्चित रूप से एक रेखा के ब्रेक को नोट नहीं करती है। – Austin

+0

यकीन है कि यह करता है। हालांकि, रेखा का एक ब्रेक निश्चित रूप से एक नई लाइन को इंगित नहीं करता है। – dvlsg

2

बस एक js लाइन:

var rows = document.querySelector('textarea').value.split("\n").length; 
+0

ओएस के आधार पर, मैंने इस के साथ समस्याओं में भाग लिया है। विशेष रूप से \ r, \ r \ n, \ n \ r, और \ n के बीच का अंतर। मुझे बिल्कुल याद नहीं है, लेकिन वास्तव में इसे पूरी तरह से करना मुश्किल था। – mike

+5

क्या होगा अगर कई रेखाएं न्यूलाइन अक्षरों की वजह से नहीं हैं, लेकिन क्योंकि एक पंक्ति अगली पंक्ति में बहती है? – brandaemon

संबंधित मुद्दे