假设PHP中有一个动态HTML创建,如下所示:
php代码foreach( $content as $obj ){
$displayString .= "<a href='' onclick='return func($obj)'> View Details </a>";
}
echo $displayString;
JSON >$content
为JSON对象数组
Javascript函数:
func()
是一个javascript函数
如何将PHP对象值传递到Javascript函数中,并允许Javascript函数通过对象解析来显示值?
如果$content是JSON格式则需要解码为数组
$content = json_decode($content,true);
foreach( $content as $obj ){// $obj is the object
$displayString .= "<a href='' onclick='return func($obj['value'])'> View Details </a>";
}
echo $displayString;
value是javascript