Цитата:
<article id="article" contenteditable="true"> <p class="paragraph">some..</p> </article> <style> p { min-height: 20px; background: red; color: white; } </style> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $('#article').keydown(function () { if (this.querySelector('p')) return; $(this).append('<p class="paragraph"></p>').find('p').focus(); }); </script> |
Цитата:
|
Да, указал..но почему то не работало..может быть какая то особенность браузера опера или потусторонние силы )
|
Цитата:
<article id="article" contenteditable="true"> <p class="paragraph">some..</p> </article> <style> p { min-height: 20px; background: red; color: white; } </style> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $('#article').keydown(function (e) { const paragraphs = this.querySelectorAll('p'); if (paragraphs.length > 1 || e.originalEvent.code !== 'Backspace') return; if (!paragraphs[0].textContent.length) e.preventDefault(); }); </script> |
Огромное спасибо!
|
contenteditable запрет удаления последнего абзаца на js
на примере кода от Nexus,
<!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style> p { min-height: 20px; background: red; color: white; } </style> <script> document.addEventListener("DOMContentLoaded", function() { document.querySelector("#article").addEventListener("keydown", function(event) { const paragraphs = this.querySelectorAll('p'); if(event.code == 'Backspace' && paragraphs.length == 1 && !paragraphs[0].textContent.length) event.preventDefault(); }) }); </script> </head> <body> <article id="article" contenteditable="true"> <p class="paragraph">some..</p> </article> </body> </html> |
И Вам большое спасибо :)
|
Часовой пояс GMT +3, время: 22:38. |