在onclick事件中我希望将变量传递给div(我使用单个div)

时间:2010-02-19 09:50:34

标签: php javascript-events

在onclick事件中如何将变量传递给div,使用该变量我希望显示div中的值。

1 个答案:

答案 0 :(得分:0)

如果使用jQuery,最好使用jQuery metadata plugins。您可以将任何值放入class属性中的DOM元素中。这是一个例子:

<div id="some_div_id" class"default {'id': '2', 'type': 'general'}">
  div content here...
</div>

<script type="text/javascript">
jQuery(function($){
  $("#some_div_id").click(function(){
    var data = $(this).metadata();
    //now you can access the variable, example:
    console.log(data.id);  //you will get 2 in Firebug
    console.log(data.type);  //you will get 'general' in Firebug
    //do some processing here...
    //...
  });
});
</script>

编写div时,您可以轻松地从php中获取值:

<div id="some_div_id" class"default {'id': '<?php echo $id; ?>', 'type': '<?php echo $type; ?>'}">

我希望这就是你要找的。

jQuery是一个跨浏览器的JavaScript库。它将使您免于编写将在所有现代浏览器中运行的复杂JavaScript代码。