javascript - 比较对象数组(图像)和跨度标签(图像)
问题描述
我想比较对象(图像)和跨度标签(图像)的数组。在按钮上单击我需要检查丢弃的图像和跨度图像是否相等。所以请帮助我。
组件.html:
<div class="field-box-samp">
<div class="captchaText">
<span id="circle_text"><img src="https://www.gstatic.com/webp/galler/1.png"></span>
<span id="triangle_text"><img src="https://www.gstatic.com/webp/galler/2.png"></span>
<span id="square_text"><img src="https://www.gstatic.com/webp/galler/3.png"></span>
</div>
</div>
组件.ts:
origin = [{
img: 'https://www.gstatic.com/webp/gallery3/1.png'
},
{
img: 'https://www.gstatic.com/webp/gallery3/2.png'
},
{
img: 'https://www.gstatic.com/webp/gallery3/3.png'
}];
//---------------
destination = [
];
//---------------
drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
} else {
let item:any = event.previousContainer.data[event.previousIndex];
let copy:any = JSON.parse(JSON.stringify(item));
let element:any = {};
for(let attr in copy){
if(attr == 'title'){
element[attr] = copy[attr] += ' copy';
}else{
element[attr] = copy[attr];
}
}
this.destination=[element];
clearimg()
}
}
}
解决方案
如果您只想检查现有跨度是否包含您的原始对象中存在的图像。
// dont use 'origin' it is also an keyword in js
let originUrl = [{
img: 'https://www.gstatic.com/webp/galler/1.png'
},
{
img: 'https://www.gstatic.com/webp/galler/2.png'
},
{
img: 'https://www.gstatic.com/webp/galler/3.png'
}];
const matchSpanFn = spanID => {
let isExist = false;
for (var i in originUrl) {
if (originUrl[i].img === document.querySelectorAll('#'+spanID+' img')[0].getAttribute('src'))
isExist = true
}
return isExist
}
matchSpanFn('circle_text') // here you can pass any id to check with constant node structure as shown above
推荐阅读
- java - 无法在android studio中更改系统时间
- c# - .NET 5:X509Certificate.GetNameInfo 大写 unicode 转义字符
- python - Python 等价于 MATLAB 的 sortrows 函数
- authentication - 如何在 JavaServer Faces 中进行安全过滤?
- git - 如何获取我强制推送的远程分支上存在的 readme.md 文件
- python - 如何在Python中使用两个标签自动计算两个不同日期之间的时间?
- sql - 按目标子句合并不更新所需的列
- javascript - 加载 iframe 并为就绪状态添加侦听器完成
- maven - 使用 Maven 运行集成测试 - 构建成功但测试运行 = 0
- firebase - HTTP 错误:403,启用服务的权限被拒绝 [cloudfunctions.googleapis.com]