查找JavaScript对象的DOM父容器
在查找已添加到JavaScript对象的图像的父容器时,是否可以遍历DOM?我有以下对象和对象代码数组:查找JavaScript对象的DOM父容器,javascript,jquery,html,object,javascript-objects,Javascript,Jquery,Html,Object,Javascript Objects,在查找已添加到JavaScript对象的图像的父容器时,是否可以遍历DOM?我有以下对象和对象代码数组: var builtVehicle = {pattern: "../images/fire1192015.png", vehicle: "../images/van1192015.png", wheel: "../images/wheels3.png"}; var createdVehicles = [ {pattern:"../images/checkerboard1192015.pn
var builtVehicle = {pattern: "../images/fire1192015.png",
vehicle: "../images/van1192015.png",
wheel: "../images/wheels3.png"};
var createdVehicles = [
{pattern:"../images/checkerboard1192015.png",
vehicle:"../images/car1192015.png", wheel:"../images/wheels1.png"},
{pattern:"../images/fire1192015.png", vehicle:"../images/truck1192015.png",
wheel:"../images/wheels4.png"},]
我在这两个元素中循环,寻找与车辆、车轮和图案属性完全匹配的图像源。如果builtVehicle对象是唯一的,即与CreatedVehicle数组中的对象不同的车辆、车轮图案图像源属性,请将其推送到数组中。但是,一旦通过createdVehicles对象的循环找到匹配项,我希望能够引用包含对象中定义的图像的div父容器。以下是我目前正在使用的功能:
builtVehicle = {
vehicle: jaVehicle,
wheel: jaWheel,
pattern: jaPattern,
}
var matchVehicle = function (vehicle, stack) {
for (var i = 0; i < stack.length; i++) {
if ((vehicle.pattern === stack[i].pattern) && (vehicle.vehicle === stack[i].vehicle) && (vehicle.wheel === stack[i].wheel)) {
//find DOM div parent that the image resides in so something like
//stack[i].closest('div'), even though that won't work
//Add some DOM manipulations
return;
}
}
// Push the vehicle to array if it didn't complete match
stack.push(vehicle);
};
matchVehicle(builtVehicle, createdVehicles);
});
我的HTML将如下所示,因此我希望能够引用div.name3以及可能的divproduct3容器:
<div class="vehiclesInBox" id="product3">
<div class="fltLeft positionRelative name3">
<img class="wheelThmbs wheelsInShipment uniqueWheel3" src="../images/wheels1.png">
<img class="pattern patternInShipment uniquePattern3" src="../images/fire1192015.png">
<img class="vehicle vehicleInShipment uniqueVehicle3" src="../images/car1192015.png">
</div>
<div class="clear"></div>
</div>
我做了一些在线搜索,但如果我想做的是可能的,或者我应该尝试不同的方法,我找不到答案。我尝试了stack[I]。最近的'div'和stack[I]。parent,但显然不起作用。提前谢谢 试试看
var filtered = $.map(createdVehicles, function(v, k) {
return $.map(builtVehicle, function(value, key) {
return v[key] === builtVehicle[key] ? value.split("/").slice(-1) : null
})
});
var res = $("img").filter(function(i, el) {
return filtered[0] === el.src.split("/").slice(-1)[0]
});
console.log(res.parents());
var builtVehicle={pattern:../images/fire192015.png,
车辆:../images/van1192015.png,
轮子:../images/wheels3.png};
var createdVehicles=[
{模式:../images/checkboard1192015.png,
车辆:../images/car1192015.png,车轮:../images/wheels1.png},
{模式:../images/fire192015.png,车辆:../images/truck192015.png,
轮子:../images/wheels4.png}];
var filtered=$.mapcreatedVehicles,functionv,k{
return$.mapbuiltVehicle,functionvalue,key{
返回v[key]==builtVehicle[key]?值.split/.slice-1:null
}
};
var res=$img.filterfunctioni,el{
返回过滤后的[0]==el.src.split/.slice-1[0]
}
console.logres.parents;
vehicle是jquery对象的数组吗?不是,vehicle是在对象builtVehicle和我正在比较的对象CreatedVehicle数组中找到的属性。