未捕获的referenceError:$ counter未定义(Uncaught referenceError: $counter is not defined)
我的JS文件中有一个函数,我希望通过一个名为$ counter的PHP变量给出一个值。 我收到一个错误:$ counter未定义。
继承人PHP代码:
<?php $counter = 0; foreach($res as $category){ echo '<div class="category" onmouseover="doChangeFontWeight($counter);" onmouseout="undoChangeFontWeight($counter);">'; echo '<p class="categoryDescription">'.utf8_encode($category['description']).'</p>'; echo '</div>'; $counter++; } ?>使用Javascript:
function doChangeFontWeight(counter) { "use strict"; document.getElementsByClassName("categoryDescription")[counter].setAttribute("style", "font-weight: 900");}I have a function in my JS file which i want to give a value by a PHP variable called $counter. I get an error: $counter is not defined.
Heres the PHP-Code:
<?php $counter = 0; foreach($res as $category){ echo '<div class="category" onmouseover="doChangeFontWeight($counter);" onmouseout="undoChangeFontWeight($counter);">'; echo '<p class="categoryDescription">'.utf8_encode($category['description']).'</p>'; echo '</div>'; $counter++; } ?>Javascript:
function doChangeFontWeight(counter) { "use strict"; document.getElementsByClassName("categoryDescription")[counter].setAttribute("style", "font-weight: 900");}最满意答案
'<div class="category" onmouseover="doChangeFontWeight('.$counter.');" onmouseout="undoChangeFontWeight('.$counter.');">';变量不会在PHP中的单引号内传递。
'<div class="category" onmouseover="doChangeFontWeight('.$counter.');" onmouseout="undoChangeFontWeight('.$counter.');">';Variables aren't passed inside single quotes in PHP.
更多推荐
发布评论