我正在填充php中的列表,并且在点击链接后使用onClick事件运行javascript函数,我需要将引擎的值与已单击的链接相关联。此时此刻通过将值分配给TITLE标记,如下面的代码所示,我知道这不是正确的方法,所以我怎样才能将值存储在页面上并能够在javascript中访问它们函数,所以根据点击的链接,它检索相应的值,我在考虑将它们存储为javascript数组或对象?或者关联的东西,所以我可以从链接的id访问它们,所以如果用e213的id点击链接,一旦点击它然后在我可以使用的javascript函数里面,'objectname'.value +'idvariable',和属性名称将是“value”+链接的id。
任何帮助都会非常感谢!
<?php
connect();
$sql = "SELECT engine_id, engine, image, value FROM engines";
$result = mysql_query($sql);
while ($row=mysql_fetch_array($result) ) {
echo "<li><a style=\"color:#FF6600;\" id='e".$row['engine_id']."' title = '".$row['value']."' onclick='return enginepic(this)' onfocus='this.hideFocus=true;' href='".$row['image']."'>".$row['engine']."</a></li>";
}
?>
答案 0 :(得分:2)
对于这种情况,HTML5允许attributes that start data-。例如...
<div data-whatever-property="some value" id="some-id"></div>
然后您可以使用以下方式获取此数据:
document.getElementById('some-id').getAttribute('data-whatever-property')
或者使用jquery ......
$('#some-id').attr('data-whatever-property')
甚至更容易......
$('#some-id').data('whateverProperty')
请注意,jQuery上的数据方法将one-two-three
变为oneTwoThree
。这符合dataset part of the html5 spec。
另外,请考虑通过JavaScript添加您的活动,而不是使用onclick / onfocus / etc属性。对于链接列表,您可以通过委派活动jquery makes these easy获得更好的效果。
答案 1 :(得分:0)
您可以将所需数据作为参数传递给enginepic
函数。
您的onclick
看起来像这样
onclick='return enginepic(this, \''" . $row['value'] . "\')
你的函数声明看起来像
function enginepic(oLink, data){
.
.
.
}