dongtan3395 2019-06-06 01:57
浏览 86

通过json_encode数组我的基本样式属性有什么问题?

I have a Javascript Calendar in an <table id="calendario">, which allows me to manipulate the table through calendario.rows[i].cells[i].

This Calendar allows the user to make a Reservation and gives the option of closing the day when the user feels it has already too many reservations.

I save his option in a boolean variable and the correspondent position, calendario.rows[i].cells[i], in a string to a SQL DB.

When I save a single result from the query in a javascript variable I can successfully change the style property color, but when I loop through a resulting array nothing changes in my page.

I have 2 columns in the "reservation" table called "Closed" (which holds the boolean 0 or 1) and "Position" (which holds the string calendario.rows[i].cells[i])

when I fetch one result of the array:

while($row = $result->fetch_assoc()) {
    $position = $row["Position"];
    }

then I save it in a Javascript variable:

<script>
var red_day =  <?php echo $position; ?>;
</script>

Then I use a function to change the style and print my result for testing purposes:

<script> 
color_red(red_day);
function color_red(position){
position.style.color = "red";
document.getElementById("result").innerHTML = position;
}
</script>

I get the last day painted in red and in the "result" <div> I get [object HTMLTableCellElement].

When I try:

$phparray = array();
 while($row = $result->fetch_assoc()) {
$phparray[] = $row["Position"];
}

Then save it in Javascript and call a function:

<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;
}

Now I don't have any cell painted in red but I get the following result in var text:

calendario.rows[4].cells[1]

calendario.rows[5].cells[5]

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongshi1207 2019-06-06 02:13
    关注

    It seems you are calling color_red() before its definition

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程