删除整个网站上的“内容”CSS属性中的“onclick”按钮。

huangapple go评论79阅读模式
英文:

Remove "content" CSS property "onclick" button for the entire website

问题

<button onclick="myFunction()">显示图片</button>

<img src="https://m.media-amazon.com/images/I/81K2hJBAduL.png">
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;
}
function myFunction() {
  document.getElementById('img');
  const element = document.styleSheets[0].cssRules[0].style.removeProperty('content');
}
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function myFunction() {
  document.getElementById(&#39;img&#39;);
  const element = document.styleSheets[0].cssRules[0].style.removeProperty(&#39;content&#39;);
}

<!-- language: lang-css -->

img {
  content: url(&quot;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&quot;);
  max-width: 100%;
  height: auto;
}

<!-- language: lang-html -->

&lt;button onclick=&quot;myFunction()&quot;&gt;Show pics&lt;/button&gt;

&lt;img src=&quot;https://m.media-amazon.com/images/I/81K2hJBAduL.png&quot;&gt;

<!-- end snippet -->

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

答案1

得分: 2

你真正需要做的是在body上设置一个类,这将影响页面上的每个图像:

<button onclick="myFunction()">显示图片</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">

编辑:我添加了代码以使类在页面加载间保持不变,使用了cookies。

注意:在StackOverflow的代码片段中,cookies受限制,因此在此处测试时不会起作用。但我在本地测试过,它确实有效。

默认情况下,cookies的过期时间是会话结束,这意味着在关闭网站时会删除cookies。要保留cookie更长时间,将天数添加到setCookie中:setCookie('permitted-images', '1', 30);(这将保留cookie 30天)。

处理JavaScript中的cookies的代码在这里找到:stackoverflow.com/a/24103596/1678383

英文:

All you really need is to set a class on the body, this will affect every image on the page:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

function myFunction() {
  document.body.classList.add(&#39;permitted&#39;);
  setCookie(&#39;permitted-images&#39;, &#39;1&#39;);
}

document.addEventListener(&#39;DOMContentLoaded&#39;, function(){
  if( getCookie(&#39;permitted-images&#39;) == &#39;1&#39; ){
    document.body.classList.add(&#39;permitted&#39;);
  }
});


/** functions for handling cookies **/
function setCookie(name,value,days) {
    var expires = &quot;&quot;;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = &quot;; expires=&quot; + date.toUTCString();
    }
    document.cookie = name + &quot;=&quot; + (value || &quot;&quot;)  + expires + &quot;; path=/&quot;;
}

function getCookie(name) {
    var nameEQ = name + &quot;=&quot;;
    var ca = document.cookie.split(&#39;;&#39;);
    for(var i=0;i &lt; ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==&#39; &#39;) c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

<!-- language: lang-css -->

body:not(.permitted) img {
  content: url(&quot;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&quot;);
  max-width: 100%;
  height: auto;
}

<!-- language: lang-html -->

&lt;button onclick=&quot;myFunction()&quot;&gt;Show pics&lt;/button&gt;
&lt;img src=&quot;https://m.media-amazon.com/images/I/81K2hJBAduL.png&quot;&gt;

&lt;img src=&quot;https://cdn.pixabay.com/photo/2023/04/02/12/40/mistletoe-7894574_1280.jpg&quot;&gt;

&lt;img src=&quot;https://cdn.pixabay.com/photo/2015/04/23/21/59/tree-736877_1280.jpg&quot;&gt;

<!-- end snippet -->

Edit: I added code to make the class persist across page loads, using cookies.

Note: Cookies are restricted in the code snippets on StackOverflow, so it will not work when testing here. But I have tested it locally, and it does work.


The default expiration of cookies is when the session ends; meaning the cookies are deleted when the website is closed. To keep the cookie for longer, add number of days to setCookie: setCookie(&#39;permitted-images&#39;, &#39;1&#39;, 30); (this will keep the cookie for 30 days).

Code for handling cookies in javascript found here: stackoverflow.com/a/24103596/1678383

答案2

得分: 0

@Rafal 你只选择样式表的第一个实例。需要遍历样式表以删除所有 "content" 属性。

例如:

&lt;script&gt;
function myFunction() {
  const styleSheets = document.styleSheets;
  for (let i = 0; i &lt; styleSheets.length; i++) {
    const cssRules = styleSheets[i].cssRules;
    for (let j = 0; j &lt; cssRules.length; j++) {
      const rule = cssRules[j];
      if (rule.style &amp;&amp; rule.style.removeProperty) {
        rule.style.removeProperty(&#39;content&#39;);
      }
    }
  }
}

</script>

英文:

@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:

&lt;script&gt;
function myFunction() {
  const styleSheets = document.styleSheets;
  for (let i = 0; i &lt; styleSheets.length; i++) {
    const cssRules = styleSheets[i].cssRules;
    for (let j = 0; j &lt; cssRules.length; j++) {
      const rule = cssRules[j];
      if (rule.style &amp;&amp; rule.style.removeProperty) {
        rule.style.removeProperty(&#39;content&#39;);
      }
    }
  }
}

</script>

huangapple
  • 本文由 发表于 2023年5月28日 14:46:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定