admin管理员组文章数量:1418035
I am currently using the below text as a simple way of hiding and displaying my text. What I was trying to do though also was use the css to display the a tag with a background image:
one to have a plus symbol for when the text is hidden.
Then one to have a minus symbol for when the text is displayed and you want to hide the text again.
JavaScript:
function toggle() {
var ele = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Show";
}
else {
ele.style.display = "block";
text.innerHTML = "Hide";
}
}
HTML:
<a id="displayText" href="javascript:toggle();">Show Further Details & Specification</a>
<div id="toggleText" style="display: none"><h1>peek-a-boo</h1></div>
I am currently using the below text as a simple way of hiding and displaying my text. What I was trying to do though also was use the css to display the a tag with a background image:
one to have a plus symbol for when the text is hidden.
Then one to have a minus symbol for when the text is displayed and you want to hide the text again.
JavaScript:
function toggle() {
var ele = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Show";
}
else {
ele.style.display = "block";
text.innerHTML = "Hide";
}
}
HTML:
<a id="displayText" href="javascript:toggle();">Show Further Details & Specification</a>
<div id="toggleText" style="display: none"><h1>peek-a-boo</h1></div>
Share
Improve this question
edited Feb 14, 2012 at 16:11
j08691
208k32 gold badges269 silver badges280 bronze badges
asked Feb 14, 2012 at 15:59
Suzi LarsenSuzi Larsen
1,5005 gold badges18 silver badges32 bronze badges
2 Answers
Reset to default 2Next time try to post your code in jsfiddle there are some issues with your code, first is better to put event handlers rather than put the javascript code in the href property, is easier to use a variable to save the state of the toggle button. Works almost 99% of the times if you show your elements with inline rather than block
var textHidden = true;
function toggle() {
var ele = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if(textHidden) {
ele.style.display = "inline";
text.innerHTML = "Show";
textHidden = false
}
else {
textHidden = true
ele.style.display = "none";
text.innerHTML = "Hide";
}
}
html:
<a id="displayText" href="#" onclick="toggle();">Show Further Details Specification</a>
<div id="toggleText" style="display:none;"><h1>peek-a-boo</h1></div>
You need to set up the CSS for "displayText" to give it a background.
Something like this:
#displayText {
display:block;
height:20px;
padding-left:30px;
background-image:url(../images/plus.png);
}
Then swap the image for the alternate state:
#displayText.active {
background-image:url(../images/minus.png);
}
So in your code you'll need to add/remove the "active" class name on displayText based on the toggle state.
P.S. It's considered bad form to used "href" for JS events. It's better to use onclick.
<a id="displayText" href="javascript://" onclick="toggle();">
本文标签: htmljavascript toggle function to hide and display texthow to add imagesStack Overflow
版权声明:本文标题:html - javascript toggle function to hide and display text - how to add images - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745283469a2651562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论