i have this code:
<?php
$i=5;
while($i > 0){
echo '
<table width="500px" border="1">
<tr>
<td>
<button id="button">Show ments</button>
</td>
</tr>
<tr>
<td id="ments" style="display:none;height:300px;width:100%;"></td>
</tr>
</table>
<script type="text/javascript">
$("#button").toggle(
function (){
$("#button").text("Hide Comments");
document.getElementById("ments").style.display="inline";
},function (){
$("#button").text("Show Comments");
document.getElementById("ments").style.display="none";
}
);
</script>
<script type="text/javascript" src="jquery.js"></script>
';
$i--;
}
?>
When the show ments button is clicked the ment box should show up. It works for the first one. But it doesn't work for the others.What's wrong?
i have this code:
<?php
$i=5;
while($i > 0){
echo '
<table width="500px" border="1">
<tr>
<td>
<button id="button">Show ments</button>
</td>
</tr>
<tr>
<td id="ments" style="display:none;height:300px;width:100%;"></td>
</tr>
</table>
<script type="text/javascript">
$("#button").toggle(
function (){
$("#button").text("Hide Comments");
document.getElementById("ments").style.display="inline";
},function (){
$("#button").text("Show Comments");
document.getElementById("ments").style.display="none";
}
);
</script>
<script type="text/javascript" src="jquery.js"></script>
';
$i--;
}
?>
When the show ments button is clicked the ment box should show up. It works for the first one. But it doesn't work for the others.What's wrong?
Share Improve this question edited Feb 4, 2013 at 4:48 user1763032 asked Feb 4, 2013 at 4:43 user1763032user1763032 4271 gold badge9 silver badges24 bronze badges 2- 2 your loops seems to output elements with identical ids... and that is invalid – Dakait Commented Feb 4, 2013 at 4:45
- 1 What has the #show id? Is that on the button? It should be either show-0, show-1, etc or a class of show. Also, you need to specify the 2nd one goes with the 2nd bit of stuff, etc. – scottheckel Commented Feb 4, 2013 at 4:46
2 Answers
Reset to default 5I'd just escape the php interpreter and print the HTML markup directly into the page. I'd also change the ids to classes, so we can more easily reuse them without quirky additional numbers.
<?php
$i=5;
while($i > 0):
?>
<table width="500px" border="1">
<tr>
<td><button class="button" data-clicked="0">Show ments</button></td>
</tr>
<tr>
<td class="ments" style="display:none;height:300px;width:100%;"></td>
</tr>
</table>
<?php
$i--;
endwhile;
?>
I don't know what the element with an ID
of show is, but we'll just assume it was the button.
$(".button").click(function (){
var $this = $(this);
if(!$this.data('clicked')){
$this.text("Hide Comments");
$this.closest('table').find('.ments').css('display', 'inline');
$this.data('clicked', 1);
}else{
$this.text("Show Comments");
$this.closest('table').find('.ments').css('display', 'none');
$this.data('clicked', 0);
}
});
Don't print that javascript in a while
loop in php, just include it in the head of the page, or in a separate file.
Edit
As indicated in a ment below,in jQuery 1.9 the toggle
no longer works as you're intending to use it, as a work around, you can add a data attribute
to the button, and check it each time we click.
<button class="button" data-clicked="0">
As you can see we've added the new data-clicked
attribute.
In our JavaScript above, you can see that we've pletely changed how it works. We perform our own if/else
to check the data-clicked
state.
It is generally good practice to separate code, markup and style declarations. As a matter of style, I prefer numbered IDs. I find they help with debugging. ymmv I also like to animate changes so people can more easily follow a change.
<!doctype html>
<html>
<head>
<title>Button Testing 1 2 3</title>
<script src="//ajax.googleapis./ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<style>
.ment {
display: none;
height: 0px;
width: 500px;
}
td {
width: 500px;
border: 1px solid #555;
border-collapse: collapse;
}
</style>
</head>
<body>
<?
$j = 0;
while ($j < 5 ) {
?>
<table>
<tr>
<td>
<button id="button_<?= $j ?>" class="button">Show ments</button>
</td>
</tr>
<tr>
<td class="ment"><span id="ment_<?= $j ?>">J = <?= $j ?></span></td>
</tr>
</table>
<?
$j++;
} ?>
<script type="text/javascript">
$(document).ready(function(){
$(".button").click(function(){
var id = $(this).attr('id');
var j = id.substr(id.indexOf('_') +1);
if ($(this).hasClass('revealed')) {
$(this).removeClass('revealed').text('Show Comments');
$('#ment_'+j).removeClass('revealed').parent().animate({'height' : 0}, function(){$(this).css({'display': 'none'})});
} else {
$(this).addClass('revealed').text('Hide Comments');
$('#ment_'+j).addClass('revealed').parent().css({'display': 'inline'}).animate({'height' : '300px'});
}
});
});
</script>
</body>
</html>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744895782a4599687.html
评论列表(0条)