最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Enable document.designMode = 'on' only for A Specific Element - Stack Overflow

programmeradmin2浏览0评论

I'm trying to figure how can I enable document.designMode = 'on' for only specific elements, for example, h1 and paragraphs, without allowing the whole website to be edited.

Example Html. (I want the JS to work on h1's and p's.)

<html>
 <head>
  <title>Hello World<title>
 </head>
 <body>
 <h1>Lorem Ipsum</h1>
  <h3>Lorem Ipsum Lorem Det Spaghet</h3>
   <p>Lorem Ipsum Det Spaghet, and kthetup super market</p>
 </body>
</html>

Javascript

document.designMode = 'on'

I'm trying to figure how can I enable document.designMode = 'on' for only specific elements, for example, h1 and paragraphs, without allowing the whole website to be edited.

Example Html. (I want the JS to work on h1's and p's.)

<html>
 <head>
  <title>Hello World<title>
 </head>
 <body>
 <h1>Lorem Ipsum</h1>
  <h3>Lorem Ipsum Lorem Det Spaghet</h3>
   <p>Lorem Ipsum Det Spaghet, and kthetup super market</p>
 </body>
</html>

Javascript

document.designMode = 'on'
Share Improve this question edited Jan 26, 2021 at 12:11 akaI-Know asked Jan 26, 2021 at 12:00 akaI-KnowakaI-Know 591 silver badge5 bronze badges 3
  • Share your code with us – Aalexander Commented Jan 26, 2021 at 12:03
  • Yes, I did the change. – akaI-Know Commented Jan 26, 2021 at 12:11
  • HTMLElement.contentEditable – pilchard Commented Jan 26, 2021 at 12:23
Add a ment  | 

2 Answers 2

Reset to default 5

Did you tried contenteditable="true" ?

<h1 contenteditable="true">Lorem Ipsum</h1>
<h3>Lorem Ipsum Lorem Det Spaghet</h3>
<p>Lorem Ipsum Det Spaghet, and kthetup super market</p>

use this code <div contenteditable="true">This text can be edited!</div> and use document.designMode = "off";

or You can apply custom settings with jQuery For example $('.title').attr('contenteditable','true');

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
  button{
    width:200px;
    height:30px;
    border:1px solid #ddd;
    border-radius:3px;
    color:white;
  }
  .color_red{
  background:red;
  }
   .color_green{
  background:green;
  }
</style>
</head>
<body>

<h1>Document designMode off Property</h1>

<p class="title">The designMode property sets whether the document is not editable or not.</p>

<p class="title">This document is not editable!</p>

   
<div class="mycls">
  This text can be edited!
</div>

<button id="setEditable" class="color_green">Set tag p Editable</button>
<button id="removeEditable" class="color_red">Remove tag p Editable</button>
<script>
document.designMode = "off";

  $('#setEditable').click(function () {
    $('.title').attr('contenteditable','true');
 });
 
 
  $('#removeEditable').click(function () {
     $('.title').attr('contenteditable','false');
 });

</script>

</body>
</html>

Test this code. it is work.

Good luck.

发布评论

评论列表(0)

  1. 暂无评论