Jump to content

Recommended Posts

Posted

Eu tenho:

<div class="pre-spoiler">
<input id="xs" value="Ver !" style="margin-left: 50px; padding: 0px; width: 80px; " onclick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = '';this.innerText = ''; this.value = 'Ocultar'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.value = 'Ver !';}" type="button"> </div>
<div>
<div class="spoiler" style="display: none;">
Aqui você coloca o texto que quer esconder e que só vai aparecer quando clicarem no botão.
</div>
</div>

e queria que ao se clicar num botão se fecha-se o botão que tem o código acima.

Podem-me ajudar?

Posted (edited)

Viva,

Começa por separar marcação, estilo e comportamento...

<html>
<head>

<!-- ESTILO -->
<style>
#xs {
	margin-left: 50px;
	padding: 0px;
	width: 80px;
}
#xs-target {
	display: none;
}
</style>
</head>
<body>
<!-- bla bla bla -->
<!-- MARCACAO -->
<div class="pre-spoiler">
<input id="xs" value="Ver !" type="button">
</div>
<div>
<div id="xs-target" class="spoiler">
	Aqui você coloca o texto que quer esconder e que só vai aparecer quando clicarem no botão.
</div>
</div>

<!-- COMPORTAMENTO -->
<script type="text/javascript">
var botao = document.getElementById('xs');
botao.addEventListener('click', function (evt) {
var target = document.getElementById('xs-target');
if (target) {
	if (target.style.display != 'block') {
		target.style.display = 'block';
		this.value = 'Ocultar';
	} else {
		target.style.display = 'none';
		this.value = 'Ver !';
	}
}
}, false);
</script>

</body>
</html>
Edited by taviroquai
  • Vote 1
Posted (edited)

Viva,

Começa por separar marcação, estilo e comportamento...

<html>
<head>

<!-- ESTILO -->
<style>
#xs {
	margin-left: 50px;
	padding: 0px;
	width: 80px;
}
#xs-target {
	display: none;
}
</style>
</head>
<body>
<!-- bla bla bla -->
<!-- MARCACAO -->
<div class="pre-spoiler">
<input id="xs" value="Ver !" type="button">
</div>
<div>
<div id="xs-target" class="spoiler">
	Aqui você coloca o texto que quer esconder e que só vai aparecer quando clicarem no botão.
</div>
</div>

<!-- COMPORTAMENTO -->
<script type="text/javascript">
var botao = document.getElementById('xs');
botao.addEventListener('click', function (evt) {
var target = document.getElementById('xs-target');
if (target) {
	if (target.style.display != 'block') {
		target.style.display = 'block';
		this.value = 'Ocultar';
	} else {
		target.style.display = 'none';
		this.value = 'Ver !';
	}
}
}, false);
</script>

</body>
</html>

Obrigado se calhar era o que utilizador CRLF queria mas não percebi...

Agora só falta clicar num botão e o botão com o texto minimizar (fechar)

Edited by Santana Oliveira
Posted (edited)

O código que coloquei funciona: faz um toggle ao element com ID xs-target.

Importa-se de dizer com se faz isso? Ainda estou a aprender Javascript...

Deste já agradeço a sua ajuda... 🙂

Edited by Santana Oliveira

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.