问题描述
在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.