求助:实现IE与Chrome兼容的可调整大小且可编辑DIV
Hey Ramaswamy, I’ve run into similar cross-browser quirks with resizable, editable DIVs before—let’s get this sorted out for you. Here’s a tested solution that works reliably in both IE (including IE11) and Chrome, with proper line wrapping while editing:
Complete Working Code
<!DOCTYPE html> <html> <head> <style> .resizable-editable-div { /* Base styling */ width: 300px; height: 200px; padding: 10px; border: 1px solid #ccc; /* Critical for automatic line wrapping during editing */ white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; /* Enable native resize in Chrome */ resize: both; overflow: auto; background-color: #fff; } /* IE-specific styling adjustments */ @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { .resizable-editable-div { overflow: auto; white-space: pre-wrap; } } </style> </head> <body> <div class="resizable-editable-div" contenteditable="true" id="editableDiv" > Type here to test line wrapping and resizing... </div> <script> const editableDiv = document.getElementById('editableDiv'); // Detect IE browser const isIE = navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > -1; if (isIE) { // Add custom resize handle for IE (since native resize is broken for contenteditable elements) const resizeHandle = document.createElement('div'); resizeHandle.style.cssText = ` position: absolute; bottom: 0; right: 0; width: 16px; height: 16px; background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'><path d='M14 14L2 2m12 0L2 14' stroke='%23ccc' stroke-width='2' fill='none'/></svg>") no-repeat center; cursor: se-resize; user-select: none; `; editableDiv.style.position = 'relative'; editableDiv.appendChild(resizeHandle); // Handle resize logic for IE let isResizing = false; let startX, startY, startWidth, startHeight; resizeHandle.addEventListener('mousedown', (e) => { isResizing = true; startX = e.clientX; startY = e.clientY; startWidth = parseInt(window.getComputedStyle(editableDiv).width, 10); startHeight = parseInt(window.getComputedStyle(editableDiv).height, 10); e.preventDefault(); }); document.addEventListener('mousemove', (e) => { if (!isResizing) return; const newWidth = startWidth + (e.clientX - startX); const newHeight = startHeight + (e.clientY - startY); // Set minimum size to avoid the div becoming too small if (newWidth > 100) editableDiv.style.width = `${newWidth}px`; if (newHeight > 100) editableDiv.style.height = `${newHeight}px`; }); document.addEventListener('mouseup', () => { isResizing = false; }); } </script> </body> </html>
Key Details & Fixes
Let me break down why this works across both browsers:
1. Editable & Line Wrapping
- We use the standard
contenteditable="true"attribute, supported by IE8+ and all modern browsers. - To ensure automatic line wrapping when the cursor reaches the div’s edge, we set three complementary CSS properties:
white-space: pre-wrap: Preserves spaces and allows line breaks—this is the core property that makes wrapping work consistently in both browsers.word-wrap: break-word: Ensures long words wrap to the next line (required for older IE versions).overflow-wrap: break-word: Modern browser equivalent ofword-wrapfor broader compatibility.
overflow: autois critical: it adds scrollbars when content overflows, and it’s required for Chrome’s nativeresizeproperty to work.
2. Resize Compatibility
- Chrome: Native
resize: bothworks out of the box as long as the element has anoverflowvalue other thanvisible. We set this directly in the CSS. - IE: Native
resizesupport is buggy forcontenteditableelements, so we added a custom resize handle with JavaScript. The handle uses an inline SVG for the drag icon (no external resources needed) and listens for mouse events to adjust the div’s width and height manually.
3. IE-Specific Tweaks
- We use a media query targeting IE to enforce consistent
white-spaceandoverflowsettings, which fixes line wrapping issues in older IE versions. - The custom resize handle is only added when IE is detected, so Chrome uses its native, smoother resize behavior.
Pro Tip: Test by typing long sentences and long words in both browsers—you’ll see the text wraps automatically, and you can drag the bottom-right corner to resize the div.
内容的提问来源于stack exchange,提问作者Ramaswamy
相关产品推荐
相关产品推荐

