未捕获的referenceError:$ counter未定义(Uncaught referenceError: $counter is not defined)

编程入门 行业动态 更新时间:2024-10-25 06:26:43
未捕获的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.

更多推荐

本文发布于:2023-08-05 06:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428790.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   counter   referenceError   Uncaught   defined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!