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

末鹿安然 提交于 2019-12-13 08:47:13

问题


in onclick event how to pass the variable to div, using that variable i want display the values in the div.


回答1:


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.



来源:https://stackoverflow.com/questions/2295316/in-onclick-event-i-want-the-pass-the-variable-to-divim-using-single-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!