英文:
Compare prop "title" and "id" of different elements if they match
问题
<div class="box-one">
<img title="element_a">
<img title="element_b">
<img title="element_c">
<img title="element_d">
</div>
<div class="box-two">
<div id="element_a"></div>
<div id="element_b"></div>
<div id="element_c"></div>
<div id="element_d"></div>
</div>
$('.box-one img').click(function() {
var elementTitle = $(this).prop('title');
$('.box-two div').each(function() {
var elementTable = $(this).prop('id');
if (elementTitle === elementTable) {
$(this).addClass('found');
}
});
});
英文:
html:
<div class="box-one">
<img title="element_a">
<img title="element_b">
<img title="element_c">
<img title="element_d">
</div>
<div class="box-two">
<div id="element_a"></div>
<div id="element_b"></div>
<div id="element_c"></div>
<div id="element_d"></div>
</div>
How can I check (with jQuery) by clicking on an <img>
at "box-one", if there is also a <div>
that machtes with the ID in "box-two"?
Example:
If I click on <img title="element a">
, the <div id="element a">
should be the only one that got an extra class called "found".
My bad try:
$('.box-one img').click(function() {
$(this).each( function(){
var elementTitle = $('.box-one img').prop('title');
var elementTable = $('.box-two div').prop('id');
// this part is a shame, sry 4 that //
if (elementTitle) == (elementTable) {
$(this).addClass("found");
}
});
});
答案1
得分: 1
<div class="box-one">
<img data-title="元素a">
<img data-title="元素b">
<img data-title="元素c">
<img data-title="元素d">
</div>
<div class="box-two">
<div id="元素a"></div>
<div id="元素b"></div>
<div id="元素c"></div>
<div id="元素d"></div>
</div>
const boxOne = document.querySelectorAll('.box-one img');
const boxTwo = document.querySelectorAll('.box-two div');
boxOne.forEach((item) => {
boxTwo.forEach(div => {
if (item.dataset.title === div.id) {
// 自定义逻辑
}
})
})
英文:
<div class="box-one">
<img data-title="element a">
<img data-title="element b">
<img data-title="element c">
<img data-title="element d">
</div>
<div class="box-two">
<div id="element a"></div>
<div id="element b"></div>
<div id="element c"></div>
<div id="element d"></div>
</div>
const boxOne = document.querySelectorAll('.box-one img');
const boxTwo = document.querySelectorAll('.box-two div');
// boxOne[0].dataset.title - get title for the first element
// boxTwo[0].id - get id for the first element
boxOne.forEach((item) => {
boxTwo.forEach(div => {
if (item.dataset.title === div.id) {
// your custom logic
}
})
})
答案2
得分: 1
尝试这样做。
我认为你需要为box-two中的每个div都使用一个循环,而不是点击的图像,这样你可以将点击的图像的标题与box-two中所有div的id进行比较。
英文:
Try this.
I think you need each loop for a divs in box-two, instead of an clicked image,
so you can compare the title of clicked image with all id of div in box-two.
$('.box-one img').click(function() {
var elementTitle = $(this).prop('title);
$('div.box-two').find('div').each( function(){
var elementTable = $('.box-two div').prop('id');
<!-- this part is a shame, sry 4 that --->
if (elementTitle) == (elementTable) {
$(this).addClass("found");
}
});
});
答案3
得分: 1
这是非常容易实现的,不需要任何 jQuery。我已经添加了一些基本的 CSS,以便可以尝试。找到下面的代码注释:
const boxOne = document.querySelector('.box-one');
// 我们在图像的父元素上添加了所谓的“委托监听器”
// 点击事件在DOM中向上“冒泡”,我们在 <div class="box-one"> 上捕获它
boxOne.addEventListener('click', function(event) {
// 接收点击的元素在 event.target 中可用
if (event.target.tagName === 'IMG' && event.target.title) { // 如果点击了图像
// 找到与点击的图像标题对应的 div
const div = document.getElementById(event.target.title);
if (div) { // 如果该 div 存在
div.classList.add('found'); // 将 'found' 添加到其类列表中
}
}
})
.box-one {
border: 1px solid #f2f2f2;
}
.box-one img {
background-color: #f2f2f2;
padding: 10px;
}
.box-two {
border: 1px solid #e8e8e8;
}
.box-two div {
border: 1px solid #ddd;
padding: 10px;
}
div.found {
border-color: green;
}
div.found::before {
content: "Found!";
}
<div class="box-one">
<img title="element a">
<img title="element b">
<img title="element c">
<img title="element d">
</div>
<div class="box-two">
<div id="element a"></div>
<div id="element b"></div>
<div id="element c"></div>
<div id="element d"></div>
</div>
英文:
This is really easy to achieve without any jQuery. I have added some basic CSS so things can be tried out. Find the code commented below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const boxOne = document.querySelector('.box-one');
// we're adding a so-called "delegate listener" on the parent element of the images
// click events "bubble" upwards in the DOM, we're catching it at <div class="box-one">
boxOne.addEventListener('click', function(event) {
// the element that received the click is available as event.target
if (event.target.tagName === 'IMG' && event.target.title) { // if an img was clicked
// find the div whose id corresponds to the title of the img that was clicked
const div = document.getElementById(event.target.title);
if (div) { // if that div exists
div.classList.add('found'); // add found to its classlist
}
}
})
<!-- language: lang-css -->
.box-one {
border: 1px solid #f2f2f2;
}
.box-one img {
background-color: #f2f2f2;
padding: 10px;
}
.box-two {
border: 1px solid #e8e8e8;
}
.box-two div {
border: 1px solid #ddd;
padding: 10px;
}
div.found {
border-color: green;
}
div.found::before {
content: "Found!";
}
<!-- language: lang-html -->
<div class="box-one">
<img title="element a">
<img title="element b">
<img title="element c">
<img title="element d">
</div>
<div class="box-two">
<div id="element a"></div>
<div id="element b"></div>
<div id="element c"></div>
<div id="element d"></div>
</div>
<!-- end snippet -->
答案4
得分: 0
我在搜索stackoverflow时找到了这个链接:https://stackoverflow.com/a/3373767/9590929
还有,使用原生JavaScript时,你不需要使用井号(#),例如document.getElementById('id_here'),但是当使用jQuery时,你需要使用井号来选择基于id的元素,就像CSS一样。
英文:
I found this while searching on SO: https://stackoverflow.com/a/3373767/9590929
Also with vanilla JavaScript, you don't need the hash (#) e.g. document.getElementById('id_here') , however when using jQuery, you do need to put hash to target elements based on id just like CSS.
答案5
得分: 0
您的if
语句有错误,可能会导致不稳定性。
$('.box-one img').click(function() {
$(this).each(function(){
var elementTitle = $('.box-one img').prop('title'); // 错误,始终会获取第一个.box-one img的标题
var elementTable = $('.box-two div').prop('id'); // 错误,始终会获取第一个.box-two div的id
if (elementTitle == elementTable) {
alert("found");
}
});
});
为了符合您的要求,您可以像这样做:
$(".box-one img").on("click", function () {
var title = $(this).prop("title");
$(".box-two #" + title).addClass("found");
});
英文:
Your if
-statement is incorrect and might cause instability.
$('.box-one img').click(function() {
$(this).each( function(){
var elementTitle = $('.box-one img').prop('title'); // wrong, will always get the title for the first .box-one img
var elementTable = $('.box-two div').prop('id'); // wrong, will always get the id for the first .box-two div
if (elementTitle == elementTable) {
alert("found");
}
});
});
To comply with your answer you could do something like this
$(".box-one img").on("click", function () {
var title = $(this).prop("title");
$(".box-two #" + title).addClass("found");
});
答案6
得分: 0
请简化并忽略容器box-two,因为您已经在box one图像属性中放置了元素ID。
$('.box-one img').click(function() {
var target_id = $(this).attr('title');
$('#' + target_id).addClass("found");
});
英文:
Please make it simple and ignore container box-two because your already put the elementId in your box one image attributes.
$('.box-one img').click(function() {
var target_id = $(this).attr('title');
$('#'+target_id).addClass("found");
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论