Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML5数据-*属性未按预期工作_Javascript_Php_Html - Fatal编程技术网

Javascript HTML5数据-*属性未按预期工作

Javascript HTML5数据-*属性未按预期工作,javascript,php,html,Javascript,Php,Html,我有一个带有HTML5 doctype的页面,我试图在我的表的元素中使用data属性,如下所示: while($res = mysql_fetch_assoc($U)){ if($c == "table-row-dark"){ $c = "table-row-light"; }else{ $c = "table-row-dark"; } echo "<tr data-id='".$res['rid']."' class='".

我有一个带有HTML5 doctype的页面,我试图在我的表的
元素中使用data属性,如下所示:

while($res = mysql_fetch_assoc($U)){
    if($c == "table-row-dark"){
        $c = "table-row-light";
    }else{
        $c = "table-row-dark";
    }
    echo "<tr data-id='".$res['rid']."' class='".$c."'>"
    ."<td>".$res['fname']." ".$res['lname']."</td>"
    ."<td>".$res['company']."</td>"
    ."<td>".$res['rcity'].", ".$res['rstate']."</td>"
    ."<td>".$res['rphone']."</td>"
    ."</tr>";
}

问题是,当发出
url
警报时,它将属性值显示为
null
。我做错了什么?如何在保持跨浏览器兼容性的同时修复它?

尝试更改
row.getAttribute('data-id')
to
this.getAttribute('data-id')

您将通过标记名获取元素,然后尝试使用jquery函数对其进行查询

this.getAttribute('data-id');

方法是这样的。

当您检查HTML时,您在数据id属性中看到了什么?另外,如果您要麻烦对PHP字符串使用双引号,对HTML使用单引号,至少要使用变量插值<代码>回声“”
等等@Jessica
也许可以尝试使用javascript连接(
.concat()
)而不是+作为字符串?我忘了在JS中是否允许在字符串上使用+了?使用插值与我现在所做的相反有什么好处?PHP可以正常工作。
this.getAttribute('data-id');