Php 使用javascript逐个字符显示标签

Php 使用javascript逐个字符显示标签,php,javascript,Php,Javascript,我正在使用PHP,MySQL和Javascript创建吊死一个人。每件事都很完美,我从DB中随机得到一个单词,将其显示为标签将其应用到一个类中,其中display=none。现在,当我点击一个字符时,这个字符会变得很好,这是我想要的,但是标签字符不会显示。 我的代码是: <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> <?php include( 'config.php

我正在使用PHPMySQLJavascript创建吊死一个人。每件事都很完美,我从DB中随机得到一个单词,将其显示为标签将其应用到一个类中,其中display=none。现在,当我点击一个字符时,这个字符会变得很好,这是我想要的,但是标签字符不会显示。 我的代码是:

<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<?php

    include( 'config.php' );

    $question = questions();    // Get question.
    $alpha = alphabats();       // Get alphabets.

?>
<script language="javascript">
    function clickMe( name ){

        var question = '<?php echo $question; ?>';
        var questionLen = <?php echo strlen($question); ?>;

        for ( var i = 0; i < questionLen; i++ ){
            if ( question[i] == name ){

                var link = document.getElementById( name );
                link.style.display = 'none';

                var label = document.getElementById( 'questionLabel' + i );
                label.style.display = 'block';

            }
        }
    }
</script>
<div>
<table align="center" style="border:solid 1px">
    <tr>
        <?php
            for ( $i = 0; $i < 26; $i++ ) {
                echo "<td><a href='#' id=$alpha[$i] name=$alpha[$i] onclick=clickMe('$alpha[$i]');>". $alpha[$i] ."</a>&nbsp;</td>";            
            }
        ?>
    </tr>
</table>
<br/>
<table align="center" style="border:solid 1px">
    <tr>
        <?php
            for ( $i = 0; $i < strlen($question); $i++ ) {
                echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";            
            }
        ?>
    </tr>
</table>
</div>

函数clickMe(名称){
var问题=“”;
变量n=;
对于(变量i=0;i

首先,当您将其显示设置为
none
时,它为什么会显示

第二,你可能想把字母隐藏在
之外,如果你不这样做,如果它在问题中出现几次,你会把字母隐藏几次(想想“香蕉”-如果你选择“a”,它会隐藏“a”三次)-这不是问题,如果信没有出现在问题中,也不会隐藏它——可能是这样

第三,为什么要使用标签?你可以,这不是违法的,但它们有一个明确的目的——标记属于复选框的文本和其他没有文本的可选元素。最好根据元素的预期含义使用它们。由于在刽子手游戏中没有专门用于单个字母的HTML元素,因此最好使用
span
div

更新: 试试这个;我不确定,但有理由相信这就是你想要的:

    for ( var i = 0; i < questionLen; i++ ){
        var link = document.getElementById( name );
        link.style.display = 'none';

        if ( question[i] == name ){

            var label = document.getElementById( 'questionLabel' + i );
            label.style.display = 'inline';

        }
    }
for(变量i=0;i
首先,当您将其显示设置为
none
时,它为什么会显示

第二,你可能想把字母隐藏在
之外,如果你不这样做,如果它在问题中出现几次,你会把字母隐藏几次(想想“香蕉”-如果你选择“a”,它会隐藏“a”三次)-这不是问题,如果信没有出现在问题中,也不会隐藏它——可能是这样

第三,为什么要使用标签?你可以,这不是违法的,但它们有一个明确的目的——标记属于复选框的文本和其他没有文本的可选元素。最好根据元素的预期含义使用它们。由于在刽子手游戏中没有专门用于单个字母的HTML元素,因此最好使用
span
div

更新: 试试这个;我不确定,但有理由相信这就是你想要的:

    for ( var i = 0; i < questionLen; i++ ){
        var link = document.getElementById( name );
        link.style.display = 'none';

        if ( question[i] == name ){

            var label = document.getElementById( 'questionLabel' + i );
            label.style.display = 'inline';

        }
    }
for(变量i=0;i
您是否尝试过
label.style.display=''
而不是
'block'

您是否尝试过
label.style.display=''
而不是
'block'

$question似乎被误用了

在这方面:

for ( $i = 0; $i < strlen($question); $i++ ) {
 echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";            
}
for($i=0;$i
你说strlen是字符串中的字符数。或者也就是字符串长度。 然后你说“…$question[$i]…”这是一个非数组

所以

将“strlen”替换为“count”,然后对$questions使用str_split

所以你最后

$question = str_split($question);
for ( $i = 0; $i < count($question); $i++ ) {
   echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";            
}
$question=str\u split($question);
对于($i=0;$i

这将分割每个字符,我认为您正试图这样做。

$问题似乎被误用了

在这方面:

for ( $i = 0; $i < strlen($question); $i++ ) {
 echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";            
}
for($i=0;$i
你说strlen是字符串中的字符数。或者也就是字符串长度。 然后你说“…$question[$i]…”这是一个非数组

所以

将“strlen”替换为“count”,然后对$questions使用str_split

所以你最后

$question = str_split($question);
for ( $i = 0; $i < count($question); $i++ ) {
   echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";            
}
$question=str\u split($question);
对于($i=0;$i
这将分割每个字符,我认为您正试图这样做。

问题(正如Amadan指出的)是您正在将标签的
显示设置为
none
(看起来您可能已经粘贴了副本):

也可以考虑重构使用正则表达式,而不是循环遍历字符串:

function clickMe(name) {

    // Get the question string
    var question = '<?php echo $question; ?>',

    // Create a RegExp based on the name
        re = new RegExp(name, "gi"),

    // Get a handle to the link   
        link = document.getElementById(name),

    // Set up our `match` variable
        match;

    // Set the link display to "none" outside of the loop
    link.style.display = "none";

    // For each match found in the question, show that label. 
    while(match = re.exec(question))
        document.getElementById("questionLabel"+match.index)
          .style.display = "inline";
}
功能点击我(名称){
//获取问题字符串
var问题=“”,
//基于名称创建一个RegExp
re=新的RegExp(名称,“gi”),
//获取链接的句柄
link=document.getElementById(名称),
//设置'match'变量
匹配;
//将循环外部的链接显示设置为“无”
link.style.display=“无”;
//对于问题中找到的每个匹配项,显示该标签。
while(match=re.exec(问题))
document.getElementById(“问题标签”+match.index)
.style.display=“内联”;
}
你的函数只压缩到