I have a Javascript function like this in my HTML file.
<script type="text/javascript">
function randomNumber() {
return Math.random();
}
</script>
And within a same file i have some php forloop.
I am generating some <tr>
from the loop
and inside a loop i want to make the call to the javascript function but its not returning anything.
Can anyone please tell me if i am doing anything wrong ?
here is my php for loop.
<?php
for($i = 0; $i < 10; $i++) {
?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <script>randomNumber();</script> </td>
</tr>
<?php
} ?>
答案 0 :(得分:1)
You should use
document.write()
to get output:
<script> document.write(randomNumber()); </script>
答案 1 :(得分:1)
You should use something like this, instead:
<script type="text/javascript">document.write(randomNumber());</script>
In any case, i discourage the use of that kind of methods, instead implement something like:
<?php
for($i = 0; $i < 10; $i++) {
?>
<tr>
<td> <?php echo $i; ?> </td>
<td class="random-number"></td>
</tr>
<?php
}
?>
<script type="text/javascript">
var rnTds = document.querySelectorAll("td.random-number");
for (i = 0; i < rnTds.length; i++) {
rnTds[i].innerHTML = randomNumber();
}
</script>
答案 2 :(得分:0)
change <script>randomNumber();</script>
To
<script>document.write(randomNumber());</script>