英文:
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("demo").innerHTML = `<image style="width= 100%; height:auto" src="https://sharepoint2.web.PNG"/>`;
document.querySelector('#testButton').innerHTML = '-';
}
<!-- language: lang-html -->
<button id="testButton" onclick="myFunction()">+</button>
<p id="demo"> </p>
<!-- 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("demo").innerHTML = '';
document.querySelector('#testButton').innerHTML = '+';
open = false;
}else{ //If now close
document.getElementById("demo").innerHTML = `<image style="width= 100%; height:auto" src="https://cdn.sstatic.net/Img/teams/teams-illo-free-sidebar-promo.svg?v=47faa659a05e"/>`;
document.querySelector('#testButton').innerHTML = '-';
open = true;
}
}
<!-- 语言: lang-html -->
<button id="testButton" onclick="myFunction()">+</button>
<p id="demo"> </p>
<!-- 结束代码片段 -->
英文:
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("demo").innerHTML = '';
document.querySelector('#testButton').innerHTML = '+';
open = false;
}else{ //If now close
document.getElementById("demo").innerHTML = `<image style="width= 100%; height:auto" src="https://cdn.sstatic.net/Img/teams/teams-illo-free-sidebar-promo.svg?v=47faa659a05e"/>`;
document.querySelector('#testButton').innerHTML = '-';
open = true;
}
}
<!-- language: lang-html -->
<button id="testButton" onclick="myFunction()">+</button>
<p id="demo"> </p>
<!-- 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("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()')
}
<!-- language: lang-html -->
<button id="testButton" onclick="myFunction()">+</button>
<p id="demo"> </p>
<!-- 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("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 = '-';
}
}
<!-- language: lang-html -->
<button id="testButton" onclick="myFunction()">+</button>
<p id="demo"> </p>
<!-- 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('#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;
}
}
<!-- language: lang-html -->
<button id="testButton" data-toggle="off" onclick="myFunction()">+</button>
<p id="demo"> </p>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论