Javascript/jQuery 范围和上下文问题,当试图操作对象的“引用”时
Javascript/jQuery Scoping and Context Issue when trying to manipulate the "reference" of an object
我想我遇到了一个"上下文"问题,希望有人能在这个问题上有更多的了解。
我想删除页面上所有图像的来源。 我为每个图像构建了一个对象数组。 当我隐藏源时,我希望更改实时显示,而不会影响我的原始对象属性。 我想在点击事件上操作图像(在屏幕上)。
例如:
$('#moz_iframe').contents().find("img").each(function(index){
aryImageObjects.push(new imageObject($(this), $('#iframeHolder')));
}); //end each
。
function imageObject(objImg, objHolder) {
this.objImg = objImg;
this.imgSrc = objImg.attr('src');
//this.objImg.replaceWith('hrm'); <-- this works just fine in this context
}; //end constructor
。但这不起作用:
$('#imagesOff').click(function(){
for (i=0; i<aryImageObjects.length; i++) {
aryImageObjects[i].objImg.replaceWith('hrm');
};
}); //end imagesOff click function
我还尝试在原始构造函数中构建一个对象方法:
this.hideImages = function() {
this.objImg.replaceWith('hrm');
};
。
$('#imagesOff').click(function(){
for (i=0; i<aryImageObjects.length; i++) {
aryImageObjects[i].hideImages();
};
}); //end imagesOff click function
但这似乎也行不通。
任何帮助将不胜感激:)
这是我
提出的
$(function() {
//reference array
var ref = [];
(function() {
//get each image and reference the element and store source
$('img').each(function() {
var newImg = {};
newImg.el = this; //storing the DOM element instead of a jQuery object
newImg.src = this.src;
ref.push(newImg);
});
}());
//if the element is clicked, replace using the reference in the array
$('img').on('click', function() {
for (i = 0; i < ref.length; i++) {
//wrap reference in jQuery, and operate
$(ref[i].el).replaceWith('hrm');
};
//img still here even after DOM was replaced
console.log(ref);
});
});
相关文章:
- javascript对象操作:根据指定条件选择属性
- 将数组对象传递到struts2中的操作类
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- 我应该如何将响应数据保存在对象(json)中以获得更好的操作和性能
- 响应应包含一个对象,但得到的却是GET操作的数组
- 如何将原型用于自定义方法和对象操作
- 以非ajax方式将js对象传递给mvc操作
- 排序和操作json对象,javascript
- 使用查询将对象数组发布到asp.net-mvc控制器操作的正确方法是什么
- Javascript对象操作
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- 如何使用jquery操作iframe对象
- JavaScript,字符串/对象操作的例外
- JavaScript 对象操作问题
- IE 是唯一不允许 Flash 对象操作的浏览器(或版本)
- 多个数组对象操作
- UTC日期对象操作
- 数组对象操作来创建新对象
- 需要javascript中字符串对象操作方面的帮助
- JavaScript中的对象操作