在onclick事件中我希望将变量传递给div(我使用单个div) [英] In onclick event i want the pass the variable to div(i'm using single div)

查看:139
本文介绍了在onclick事件中我希望将变量传递给div(我使用单个div)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

如果您使用jQuery,最好使用 jQuery元数据插件。您可以将任何值放入属性中的DOM元素中。以下是示例:

 < div id =some_div_idclassdefault {'id':'2','输入':'general'}> 
div div here here ...
< / div>

< script type =text / javascript>
jQuery(function($){
$(#some_div_id)。click(function(){
var data = $(this).metadata();
/ /现在你可以访问变量,例如:
console.log(data.id); //你将在Firebug获得2
console.log(data.type); //你会得到' general'in Firebug
//在这里做一些处理......
// ...
});
});
< / script>

当写 div 时,你可以把来自php的价值很容易:

 < div id =some_div_idclassdefault {'id':'<? php echo $ id;?>','type':'<?php echo $ type;?>'}> 

我希望这就是你要找的。

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


in onclick event how to pass the variable to div, using that variable i want display the values in the 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.

这篇关于在onclick事件中我希望将变量传递给div(我使用单个div)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆