javascript event内如果获取外部的值 本帖最后由 yunfeifan 于 2011-11-10 16:00:52 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 alert(me.value) 其实没看懂,猜的 me 我已经编辑,不能使用me,me是最后一个object的值。 <div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div><div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>function test(div, value){ this.div_ = document.getElementById(div); this.value_ = value; me = this; this.div_.onclick = (function(){ return function(obj){ alert(this.id); alert(this.style.cssText); } })(this.div_)}window.onload = function(){ test("test1", 1); // alert(1) test("test2", 2); // alert(2)}</script> <div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div><div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>function test(div, value){ this.div_ = document.getElementById(div); this.value_ = value; me = this; this.div_.onclick = (function(obj){ return function(){ alert(obj.id); alert(obj.style.cssText); } })(this.div_)}window.onload = function(){ test("test1", 1); // alert(1) test("test2", 2); // alert(2)}</script> 谢谢hookee但是我并不是想要获取div_对应的内容,而是div对应的object的属性值,或许调用object的一些方法。 一样道理<div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div><div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>function test(div, value){ this.div_ = document.getElementById(div); this.value_ = value; me = this; this.div_.onclick = (function(obj){ return function(){ alert(obj.div_.id); alert(obj.value_); } })(this)}window.onload = function(){ test("test1", 1); // alert(1) test("test2", 2); // alert(2)}</script> 这样的?<body><div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div><div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>function test(div, value){ div_ = document.getElementById(div); div_.value_ = value; div_.onclick = function(evt){ ele = window.event?window.event.srcElement:evt.target; alert(ele.value_) }}test("test1", 1); // alert(1)test("test2", 2); // alert(2)</script> 就是一个对象,里边有很多值,其中有一个是一个div, div有一个onclick事件,在事件内部我想获取此对象的所有的属性(或者调用此对象的一些方法),有好的办法吗? <div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div><div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>function test(div, value){ this.div_ = document.getElementById(div); this.value_ = value; var me = this; this.div_.onclick = function(){ // alert(me.value_) 获取当前object的属性值 }}test("test1", 1); // alert(1)test("test2", 2); // alert(2)</script>还是可以的 关于input中自动添加内容后光标定位的问题! jQuery 的live不支持FireFox? 求助,急!!Word.Application文件路径为相对路径 IE7中使用window.open是否有参数modal=yes|no 如何去掉滚动条? 我的一张表格,显示数据库的记录,我想点击哪个标题栏,就按照哪个标题排序,行吗? 问一个巨简单的问题?大家都来捧捧场啊! 怎样清空<input type='text'>中的数据 一个关于NaN的问题 如何confirm-history.back()之后终断程序运行 JQuery和JS获取ID的方式有区别么 extjs中在gridpanel中添加查询框
其实没看懂,猜的
<div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div>
<div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>
function test(div, value){
this.div_ = document.getElementById(div);
this.value_ = value;
me = this;
this.div_.onclick = (function(){
return function(obj){
alert(this.id);
alert(this.style.cssText);
}
})(this.div_)
}
window.onload = function(){
test("test1", 1); // alert(1)
test("test2", 2); // alert(2)
}
</script>
<div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div>
<div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>
function test(div, value){
this.div_ = document.getElementById(div);
this.value_ = value;
me = this;
this.div_.onclick = (function(obj){
return function(){
alert(obj.id);
alert(obj.style.cssText);
}
})(this.div_)
}
window.onload = function(){
test("test1", 1); // alert(1)
test("test2", 2); // alert(2)
}
</script>
<div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>
function test(div, value){
this.div_ = document.getElementById(div);
this.value_ = value;
me = this;
this.div_.onclick = (function(obj){
return function(){
alert(obj.div_.id);
alert(obj.value_);
}
})(this)
}
window.onload = function(){
test("test1", 1); // alert(1)
test("test2", 2); // alert(2)
}
</script>
<body>
<div style='border: 1px solid #000;width:100px; height: 100px;background: red' id="test1"></div>
<div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>
function test(div, value){
div_ = document.getElementById(div);
div_.value_ = value;
div_.onclick = function(evt){
ele = window.event?window.event.srcElement:evt.target;
alert(ele.value_)
}
}test("test1", 1); // alert(1)
test("test2", 2); // alert(2)
</script>
<div style='border: 1px solid #000;width:100px; height: 100px;background: green' id="test2"></div><script>
function test(div, value){
this.div_ = document.getElementById(div);
this.value_ = value;
var me = this;
this.div_.onclick = function(){
// alert(me.value_) 获取当前object的属性值
}
}test("test1", 1); // alert(1)
test("test2", 2); // alert(2)
</script>还是可以的