javascript - 我通过 json_encode 数组的基本样式属性有什么问题?
问题描述
我有一个 Javascript 日历<table id="calendario">
,它允许我通过calendario.rows[i].cells[i]
.
此日历允许用户进行预订,并在用户觉得已经有太多预订时提供关闭一天的选项。
我将他的选项保存在布尔变量中,并将对应的位置 , 保存到SQL DBcalendario.rows[i].cells[i]
的字符串中。
当我将查询的单个结果保存在 javascript 变量中时,我可以成功更改样式属性颜色,但是当我遍历结果数组时,我的页面中没有任何变化。
我在表中有 2 列"reservation"
称为"Closed"
(包含布尔值 0 或 1)和"Position"
(包含字符串 calendario.rows[i].cells[i])
当我获取数组的一个结果时:
while($row = $result->fetch_assoc()) {
$position = $row["Position"];
}
然后我将它保存在一个 Javascript 变量中:
<script>
var red_day = <?php echo $position; ?>;
</script>
然后我使用一个函数来更改样式并打印我的结果以进行测试:
<script>
color_red(red_day);
function color_red(position){
position.style.color = "red";
document.getElementById("result").innerHTML = position;
}
</script>
我把最后一天涂成红色,在“结果”中<div>
我得到了[object HTMLTableCellElement]
。
当我尝试:
$phparray = array();
while($row = $result->fetch_assoc()) {
$phparray[] = $row["Position"];
}
然后将其保存在 Javascript 中并调用一个函数:
<script>
var jsarray = <?php echo json_encode($phparray); ?>;
result(jsarray){
var text, pLen, i;
pLen = jsarray.length;
text = "<ul>";
for (i = 0; i < pLen; i++) {
jsarray[i].style.color = "red";
text += "<li>" + jsarray[i] + "</li>";
}
text += "</ul>";
document.getElementById("result").innerHTML = text;
}
现在我没有任何单元格被涂成红色,但我在 var 文本中得到以下结果:
calendario.rows[4].cells[1]
calendario.rows[5].cells[5]
我究竟做错了什么?
解决方案
看来您在定义之前调用 color_red()
推荐阅读
- location - 在用户位置初始化 mapbox 地图
- c - 生产者消费者问题问题使用 Linux 线程
- javascript - 反应未正确设置 ref,未捕获 TypeError:current.focus 不是函数,无法聚焦元素
- javascript - 比较两个不同用户请求的两个 JSON API 响应。Spotify API
- c# - 创建在 system.net/defaultproxy 中指定的 Web 代理时出错
- bash - 如何使用 Bash 脚本检查未知文件是否存在
- python - Python pygame - 弹跳球(UnboundLocalError:分配前引用的局部变量'move_y')
- flutter - Flutter 按钮不会将状态从 active=true 更改为 active=false
- python - python中看不见的数据的分类数据编码器
- java - 从 GraphQL 查询中获取键值