2011-08-21 18 views
34

मैं div तत्व में लंबवत पाठ ओवरफ़्लो का पता कैसे लगा सकता हूं?div तत्व में अतिप्रवाह का पता लगाने के लिए कैसे?

सीएसएस:

div.rounded { 
    background-color:#FFF; 
    height: 123px; 
    width:200px; 
    font-size:11px; 
    overflow:hidden; 
} 

HTML:

<div id="tempDiv" class="rounded"> 
    Lorem ipsum dolor sit amet, 
    consectetur  adipiscing elit. Phasellus vel quam vestibulum orci blandit laoreet. 
</div> 
+2

वास्तव में "पता लगाने" का क्या मतलब है? प्रतिक्रिया में आप क्या करना चाहते हैं, स्क्रॉलबार दिखाएं? –

+0

यदि टेक्स्ट ओवरफ्लो हो जाता है तो मैं माउस होवर पर div का आकार बदलना चाहता हूं लेकिन मैंने पहले से ही इसे हल किया है, इसलिए यह प्रश्न का हिस्सा नहीं था। –

+0

महान उत्तर के साथ इसी तरह का पुराना प्रश्न: https://stackoverflow.com/a/143889/573057 – earcam

उत्तर

45

आप easil कर सकते हैं

<script type="text/javascript"> 
function GetContainerSize() 
{ 
    var container = document.getElementById ("tempDiv"); 
    var message = "The width of the contents with padding: " + container.scrollWidth + "px.\n"; 
    message += "The height of the contents with padding: " + container.scrollHeight + "px.\n"; 

    alert (message); 
} 
</script> 

अधिक जानकारी के लिए पर एक नज़र डालें: http://help.dottoro.com/ljbixkkn.php

+0

यह काम नहीं करता है अगर div पर ओवरफ़्लो नियम 'दृश्यमान' है, जो डिफ़ॉल्ट है। फ़ायरफ़ॉक्स का उपयोग करना 18. – Ryan

+0

यह आपको नहीं बताता कि कौन सा विशिष्ट div बह रहा है। – NoBugs

+0

इस समाधान के साथ समस्या यह है कि यदि तत्व छिपा हुआ है (या यह माता-पिता) दोनों 0 लौटते हैं .. कोई कामकाज? –

5

निम्नलिखित jQuery प्लगइन परिणाम सूचित करेंगे।

सीएसएस

#tempDiv{ 
    height:10px; 
    overflow:hidden; 
}​ 

चौड़ाई में अतिप्रवाह निर्धारित करने के लिए,

(function($) { 
    $.fn.isOverflowWidth = function() { 
     return this.each(function() { 
      var el = $(this); 
      if (el.css("overflow") == "hidden") { 
       var text = el.html(); 
       var t = $(this.cloneNode(true)).hide().css('position', 'absolute').css('overflow', 'visible').width('auto').height(el.height()); 
       el.after(t);  
       function width() { 
        return t.width() > el.width(); 
       }; 
       alert(width()); 
      } 
     }); 
    }; 
})(jQuery); 

ऊंचाई में अतिप्रवाह निर्धारित करने के लिए,

(function($) { 
    $.fn.isOverflowHeight = function() { 
     return this.each(function() { 
      var el = $(this); 
      if (el.css("overflow") == "hidden") { 
       var text = el.html(); 
       var t = $(this.cloneNode(true)).hide().css('position', 'absolute').css('overflow', 'visible').height('auto').width(el.width()); 
       el.after(t); 

       function height() { 
        return t.height() > el.height(); 
       }; 
       alert(height()); 
      } 
     }); 
    }; 
})(jQuery); 

http://jsfiddle.net/C3hTV/

+2

यह सुनिश्चित नहीं है कि यह जांचने का सबसे प्रभावी या सबसे अच्छा तरीका है - क्या होगा यदि उस विशेष div के भीतर स्क्रिप्ट है, तो यह फिर से चलाएगा यह – NoBugs

2

Chamika के जवाब पर एक बदलाव है, तो आपका वास्तविक html में, एक आंतरिक है y है कि clientHeight साथ scrollHeight तुलना करके, निम्न प्रयास करें और बाहरी div। बाहरी div में स्थिर ऊंचाई और चौड़ाई और अतिप्रवाह छिपा होगा। आंतरिक div में केवल सामग्री है और सामग्री तक फैल जाएगी।

फिर आप गतिशील रूप से कुछ भी जोड़ने की आवश्यकता के बिना, 2 divs की ऊंचाई और चौड़ाई की तुलना कर सकते हैं।

<div id="tempDiv" class="rounded"> 
    <div class="content"> 
     Lorem ipsum dolor sit amet, 
     consectetur  adipiscing elit. Phasellus vel quam vestibulum orci blandit laoreet. 
    </div> 
</div> 
संबंधित मुद्दे