在HTML中PNG的可折叠单击函数

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

Collapsible Onclick function for PNG in HTML

问题

I am trying to build a collapsible onclick function in HTML to display an image upon clicking a plus sign to fold out to a src image.
我正在尝试在HTML中构建一个可折叠的onclick函数,以便在点击加号时显示一张图像,以展开到一个src图像。

I have accomplished this, but need to program the function to collapse and close the image once I click onto the "-" sign.
我已经完成了这一点,但需要编程使该函数在我点击"-"符号时折叠和关闭图像。

Can someone please help me with this?
请有人帮助我吗?

英文:

I am trying to build a collapsible onclick function in HTML to display an image upon clicking a plus sign to fold out to a src image.
I have accomplished this, but need to program the function to collapse and close the image once I click onto the "-" sign.

Can someone please help me with this?

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

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

function myFunction() {
  document.getElementById(&quot;demo&quot;).innerHTML = `&lt;image style=&quot;width= 100%; height:auto&quot; src=&quot;https://sharepoint2.web.PNG&quot;/&gt;`;
  document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;-&#39;;
}

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

&lt;button id=&quot;testButton&quot; onclick=&quot;myFunction()&quot;&gt;+&lt;/button&gt;
&lt;p id=&quot;demo&quot;&gt; &lt;/p&gt;

<!-- end snippet -->

答案1

得分: 1

我找不到你的图片。所以我使用stackoverflow网站的图像。只需更改那个。

<!-- 开始代码片段: JavaScript 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

    var open = false; //Point

    function myFunction() {   
      if(open == true){ //If now open
        document.getElementById(&quot;demo&quot;).innerHTML = &#39;&#39;;
        document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;+&#39;; 
        open = false;
      }else{             //If now close
        document.getElementById(&quot;demo&quot;).innerHTML = `&lt;image style=&quot;width= 100%; height:auto&quot; src=&quot;https://cdn.sstatic.net/Img/teams/teams-illo-free-sidebar-promo.svg?v=47faa659a05e&quot;/&gt;`; 
        document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;-&#39;; 
        open = true;
      }    
     }

<!-- 语言: lang-html -->

    &lt;button id=&quot;testButton&quot; onclick=&quot;myFunction()&quot;&gt;+&lt;/button&gt; 
    &lt;p id=&quot;demo&quot;&gt; &lt;/p&gt;

<!-- 结束代码片段 -->
英文:

I can't find your image. So, I use stackoverflow site's img. just change that.

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

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

var open = false; //Point

function myFunction() {   
  if(open == true){ //If now open
    document.getElementById(&quot;demo&quot;).innerHTML = &#39;&#39;;
    document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;+&#39;; 
    open = false;
  }else{             //If now close
    document.getElementById(&quot;demo&quot;).innerHTML = `&lt;image style=&quot;width= 100%; height:auto&quot; src=&quot;https://cdn.sstatic.net/Img/teams/teams-illo-free-sidebar-promo.svg?v=47faa659a05e&quot;/&gt;`; 
    document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;-&#39;; 
    open = true;
  }    
 }

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

&lt;button id=&quot;testButton&quot; onclick=&quot;myFunction()&quot;&gt;+&lt;/button&gt; 
&lt;p id=&quot;demo&quot;&gt; &lt;/p&gt;

<!-- end snippet -->

答案2

得分: 0

以下是您要翻译的内容:

You need to track whether the button has been toggled or not, you can do this in various ways.

In this example the onclick attribute on the HTML element is changed to run a different function:

function myFunction() {
  document.getElementById("demo").innerHTML = `<image style="width= 100%; height:auto" src="https://sharepoint2.web.PNG"/>`;
  document.querySelector('#testButton').innerHTML = '-';
  document.querySelector('#testButton').setAttribute('onclick','hideImage()')
}

function hideImage() {
  document.getElementById("demo").innerHTML = ``;
  document.querySelector('#testButton').innerHTML = '+';
  document.querySelector('#testButton').setAttribute('onclick','myFunction()')
}
<button id="testButton" onclick="myFunction()">+</button>
<p id="demo"> </p>

In this example there is a global variable which tracks if the button has been toggled:

let toggle = false;
function myFunction() {
  if (toggle) {
    toggle = false;
    document.getElementById("demo").innerHTML = ``;
    document.querySelector('#testButton').innerHTML = '+';
  }
  else {
    toggle = true;
    document.getElementById("demo").innerHTML = `<image style="width= 100%; height:auto" src="https://sharepoint2.web.PNG"/>`;
    document.querySelector('#testButton').innerHTML = '-';
  }
}
<button id="testButton" onclick="myFunction()">+</button> 
<p id="demo"> </p>

In this example we use an HTML data attribute to track the button toggle:

function myFunction() {
  const testButton = document.querySelector('#testButton');
  switch(testButton.dataset.toggle) {
    case "on":
      testButton.dataset.toggle = "off";
      document.getElementById("demo").innerHTML = ``;
      document.querySelector('#testButton').innerHTML = '+';
      break;
    case "off":
      testButton.dataset.toggle = "on";
      document.getElementById("demo").innerHTML = `<image style="width= 100%; height:auto" src="https://sharepoint2.web.PNG"/>`;
      document.querySelector('#testButton').innerHTML = '-';
      break;
  }
}
<button id="testButton" data-toggle="off" onclick="myFunction()">+</button>
<p id="demo"> </p>
英文:

You need to track whether the button has been toggled or not, you can do this in various ways.

In this example the onclick attribute on the HTML element is changed to run a different function:

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

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

function myFunction() {
  document.getElementById(&quot;demo&quot;).innerHTML = `&lt;image style=&quot;width= 100%; height:auto&quot; src=&quot;https://sharepoint2.web.PNG&quot;/&gt;`;
  document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;-&#39;;
  document.querySelector(&#39;#testButton&#39;).setAttribute(&#39;onclick&#39;,&#39;hideImage()&#39;)
}

function hideImage() {
  document.getElementById(&quot;demo&quot;).innerHTML = ``;
  document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;+&#39;;
  document.querySelector(&#39;#testButton&#39;).setAttribute(&#39;onclick&#39;,&#39;myFunction()&#39;)
}

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

&lt;button id=&quot;testButton&quot; onclick=&quot;myFunction()&quot;&gt;+&lt;/button&gt;
&lt;p id=&quot;demo&quot;&gt; &lt;/p&gt;

<!-- end snippet -->

In this example there is a global variable which tracks if the button has been toggled:

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

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

let toggle = false;
function myFunction() {
  if (toggle) {
    toggle = false;
    document.getElementById(&quot;demo&quot;).innerHTML = ``;
    document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;+&#39;;
  }
  else {
    toggle = true;
    document.getElementById(&quot;demo&quot;).innerHTML = `&lt;image style=&quot;width= 100%; height:auto&quot; src=&quot;https://sharepoint2.web.PNG&quot;/&gt;`;
    document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;-&#39;;
  }
}

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

&lt;button id=&quot;testButton&quot; onclick=&quot;myFunction()&quot;&gt;+&lt;/button&gt; 
&lt;p id=&quot;demo&quot;&gt; &lt;/p&gt;

<!-- end snippet -->

In this example we use a html data attribute to track the button toggle:

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

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

function myFunction() {
  const testButton = document.querySelector(&#39;#testButton&#39;);
  switch(testButton.dataset.toggle) {
    case &quot;on&quot;:
      testButton.dataset.toggle = &quot;off&quot;;
      document.getElementById(&quot;demo&quot;).innerHTML = ``;
      document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;+&#39;;
      break;
    case &quot;off&quot;:
      testButton.dataset.toggle = &quot;on&quot;;
      document.getElementById(&quot;demo&quot;).innerHTML = `&lt;image style=&quot;width= 100%; height:auto&quot; src=&quot;https://sharepoint2.web.PNG&quot;/&gt;`;
      document.querySelector(&#39;#testButton&#39;).innerHTML = &#39;-&#39;;
      break;
  }
}

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

&lt;button id=&quot;testButton&quot; data-toggle=&quot;off&quot; onclick=&quot;myFunction()&quot;&gt;+&lt;/button&gt;
&lt;p id=&quot;demo&quot;&gt; &lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 09:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378137.html
匿名

发表评论

匿名网友

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

确定