简体   繁体   中英

In onclick event I want the pass the variable to div (I'm using single div)

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

If you use jQuery, it's better to use jQuery metadata plugins . You can put any value into a DOM element inside the class attribute. Here is the example:

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

When writing the div , you can put value from the php easily:

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

I hope this is what you looking for.

jQuery is a cross browser javascript library. It will save you from writing a complex javascript code that will run in all modern browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM