比较不同元素的”标题”和”id”属性是否匹配。

huangapple go评论70阅读模式
英文:

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) {
      // 自定义逻辑
    }
  })
})
英文:
&lt;div class=&quot;box-one&quot;&gt;
 &lt;img data-title=&quot;element a&quot;&gt;
 &lt;img data-title=&quot;element b&quot;&gt;
 &lt;img data-title=&quot;element c&quot;&gt;
 &lt;img data-title=&quot;element d&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;box-two&quot;&gt;
 &lt;div id=&quot;element a&quot;&gt;&lt;/div&gt;
 &lt;div id=&quot;element b&quot;&gt;&lt;/div&gt;
 &lt;div id=&quot;element c&quot;&gt;&lt;/div&gt;
 &lt;div id=&quot;element d&quot;&gt;&lt;/div&gt;
&lt;/div&gt;



const boxOne = document.querySelectorAll(&#39;.box-one img&#39;);
const boxTwo = document.querySelectorAll(&#39;.box-two div&#39;);

// boxOne[0].dataset.title - get title for the first element
// boxTwo[0].id - get id for the first element

    boxOne.forEach((item) =&gt; {
      boxTwo.forEach(div =&gt; {
        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.

$(&#39;.box-one img&#39;).click(function() {
    var elementTitle  = $(this).prop(&#39;title);

    $(&#39;div.box-two&#39;).find(&#39;div&#39;).each( function(){
        var elementTable = $(&#39;.box-two div&#39;).prop(&#39;id&#39;);

        &lt;!-- this part is a shame, sry 4 that ---&gt;
        if (elementTitle) == (elementTable) {
            $(this).addClass(&quot;found&quot;);
        }

    });
});

答案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(&#39;.box-one&#39;);

// we&#39;re adding a so-called &quot;delegate listener&quot; on the parent element of the images
// click events &quot;bubble&quot; upwards in the DOM, we&#39;re catching it at &lt;div class=&quot;box-one&quot;&gt;
boxOne.addEventListener(&#39;click&#39;, function(event) {
  // the element that received the click is available as event.target
  if (event.target.tagName === &#39;IMG&#39; &amp;&amp; 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(&#39;found&#39;); // 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: &quot;Found!&quot;;
}

<!-- language: lang-html -->

&lt;div class=&quot;box-one&quot;&gt;
  &lt;img title=&quot;element a&quot;&gt;
  &lt;img title=&quot;element b&quot;&gt;
  &lt;img title=&quot;element c&quot;&gt;
  &lt;img title=&quot;element d&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;box-two&quot;&gt;
  &lt;div id=&quot;element a&quot;&gt;&lt;/div&gt;
  &lt;div id=&quot;element b&quot;&gt;&lt;/div&gt;
  &lt;div id=&quot;element c&quot;&gt;&lt;/div&gt;
  &lt;div id=&quot;element d&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- 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.

$(&#39;.box-one img&#39;).click(function() {
    $(this).each( function(){

        var elementTitle = $(&#39;.box-one img&#39;).prop(&#39;title&#39;); // wrong, will always get the title for the first .box-one img
        var elementTable = $(&#39;.box-two div&#39;).prop(&#39;id&#39;); // wrong, will always get the id for the first .box-two div

        if (elementTitle == elementTable) {
            alert(&quot;found&quot;);
        }

    });
});

To comply with your answer you could do something like this

$(&quot;.box-one img&quot;).on(&quot;click&quot;, function () {
    var title = $(this).prop(&quot;title&quot;);
    $(&quot;.box-two #&quot; + title).addClass(&quot;found&quot;);
});

答案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.

$(&#39;.box-one img&#39;).click(function() {
     var target_id = $(this).attr(&#39;title&#39;);
     $(&#39;#&#39;+target_id).addClass(&quot;found&quot;);
});

huangapple
  • 本文由 发表于 2020年1月3日 16:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定