jQuery和CSS从PHP生成时不起作用


jQuery and CSS not working when generated from PHP

我正在尝试创建一个关注按钮(如Twitter),它在很大程度上有效,但是当我从PHP脚本生成HTML时,它不是。

这是显示它工作的小提琴:http://jsfiddle.net/MCam435/HpAWH/10/

:)工作得很好

但是当我从 PHP 生成它时:

PHP 脚本

function getTalent($row) {
     return "<div class='talent_result_wrapper' data-experience='" . $row['divTagExp'] . "' data-salary='" . $row['divTagSal'] . "'>
      <div class='talent_result_header'>
        <span class='talent_result_head'>ID: </span>" . $row['CandidateID'] . "
      </div>
        <ul>
          <li><strong>Resides:  </strong>" . $row['Town'] . "</li>
          <li><strong>Salary Required:  </strong>£" . $row['SalaryMin'] . "</li>
          <li><strong>Experience:  </strong>" . $row['CandidateExperience'] . " Years </li>
          <li><strong>Industy:  </strong>" . $row['PrimarySector'] . "</li>
          <li><strong>Specialism:  </strong>" . $row['PrimarySector'] . "</li>
          <br>
          <div id='follow1'><a href='#' class='follow' id='1'><span class='follow_b'> + Tag </span></a></div>
          <div id='remove1' style='display:none'><a href='' class='remove' id='1'><span class='remove_b'> - UnTag </span></a></div>
      </div>";
    }

主页

while($row = mysqli_fetch_array($result2)) {
                echo getTalent($row);
            }   

div 之间的切换不起作用,即使输出完全相同?

一个页面上不能有多个具有相同 ID 的元素。

     <div id='follow1'><a href='#' class='follow' id='1'><span class='follow_b'> + Tag </span></a></div>
     <div id='remove1' style='display:none'><a href='' class='remove' id='1'><span class='remove_b'> - UnTag </span></a></div>

您需要为这些元素提供所有唯一 ID。 如果页面上只有 1 个实例,它将起作用,但是一旦您添加超过 1 个,它就会开始失败。 这就是为什么它在你的例子中有效,但在你使用 PHP 时不行。