skip to Main Content
function myFunction() {
  document.getElementById('img');
  const element = document.styleSheets[0].cssRules[0].style.removeProperty('content');
}
img {
  content: url("https://static.vecteezy.com/system/resources/previews/005/476/277/original/under-18-forbidden-round-icon-sign-illustration-eighteen-or-older-persons-adult-content-18-plus-only-rating-isolated-on-white-background-free-vector.jpg");
  max-width: 100%;
  height: auto;
}
<button onclick="myFunction()">Show pics</button>

<img src="https://m.media-amazon.com/images/I/81K2hJBAduL.png">

The point is that after clicking on the "delete" button, the "content" property for the entire website.

2

Answers


  1. @Rafal You’re only selecting the first instance of the stylesheet. It would help if you iterated through the stylesheet to remove all instances of the "content" property.

    For example:

    <script>
    function myFunction() {
      const styleSheets = document.styleSheets;
      for (let i = 0; i < styleSheets.length; i++) {
        const cssRules = styleSheets[i].cssRules;
        for (let j = 0; j < cssRules.length; j++) {
          const rule = cssRules[j];
          if (rule.style && rule.style.removeProperty) {
            rule.style.removeProperty('content');
          }
        }
      }
    }
    
    Login or Signup to reply.
  2. Your solution seems a little overengineered.

    Consider this simplified solution:

    function myFunction() {
      document.body.classList.add('permitted');
    }
    body:not(.permitted) img {
      content: url("https://static.vecteezy.com/system/resources/previews/005/476/277/original/under-18-forbidden-round-icon-sign-illustration-eighteen-or-older-persons-adult-content-18-plus-only-rating-isolated-on-white-background-free-vector.jpg");
      max-width: 100%;
      height: auto;
    }
    <button onclick="myFunction()">Show pics</button>
    <img src="https://m.media-amazon.com/images/I/81K2hJBAduL.png">
    
    <img src="https://cdn.pixabay.com/photo/2023/04/02/12/40/mistletoe-7894574_1280.jpg">
    
    <img src="https://cdn.pixabay.com/photo/2015/04/23/21/59/tree-736877_1280.jpg">
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search