Jquery Toggle 如果 .text() 等于 img[title] 则更改图像

发布于 2024-09-16 22:36:51 字数 1553 浏览 8 评论 0原文

在上周社区的帮助下,我成功实现了这个 代码在这里

现在我需要做类似的事情,但现在将 html 文本与 [title] 进行比较。到目前为止,我的代码如下:

$(document).ready(function() {

 $(".views-field-field-success-stories-image-data .field-content").click(function() {
   var sharedata = $(this).text();
   $('#bannerContainer img[title = "'+sharedata+'" ]').addClass('selected');
 });
 $("#bannerContainer img").click(function() {
  if (
   $('#bannerContainer img').hasClass('selected', $('#bannerContainer img').removeClass('selected') ) );

 });
});


      <span class="field-content">The Grand Event Center</span>
      <span class="field-content">Choura Events Catering</span>
      <span class="field-content">El Dorado Park Golf Course</span>

<div id="bannerContainer">
 <img src="1.jpg" title="The Grand Event Center" class="selected">
 <img src="1.jpg" title="The Grand Event Center" class="selected"> 
 <img src="1.jpg" title="The Grand Event Center" class="selected">
</div>

<!--- My Goal
<div id="bannerContainer">
 <img src="1.jpg" title="The Grand Event Center" class="">
 <img src="1.jpg" title="The Grand Event Center" class=""> 
 <img src="1.jpg" title="The Grand Event Center" class="selected">
</div> ---->

由于某种原因,我无法将我的头围绕在toggleClass上。

目前,如果我单击任何一个跨度,“选定的”会粘住,并且当我单击任何其他跨度时不会离开。

我缺少什么?

提前致谢!

With help from the community last week, I successfully implemented this code here

Now i need to do something similar but now compare html text with [title]. My code so far is below:

$(document).ready(function() {

 $(".views-field-field-success-stories-image-data .field-content").click(function() {
   var sharedata = $(this).text();
   $('#bannerContainer img[title = "'+sharedata+'" ]').addClass('selected');
 });
 $("#bannerContainer img").click(function() {
  if (
   $('#bannerContainer img').hasClass('selected', $('#bannerContainer img').removeClass('selected') ) );

 });
});


      <span class="field-content">The Grand Event Center</span>
      <span class="field-content">Choura Events Catering</span>
      <span class="field-content">El Dorado Park Golf Course</span>

<div id="bannerContainer">
 <img src="1.jpg" title="The Grand Event Center" class="selected">
 <img src="1.jpg" title="The Grand Event Center" class="selected"> 
 <img src="1.jpg" title="The Grand Event Center" class="selected">
</div>

<!--- My Goal
<div id="bannerContainer">
 <img src="1.jpg" title="The Grand Event Center" class="">
 <img src="1.jpg" title="The Grand Event Center" class=""> 
 <img src="1.jpg" title="The Grand Event Center" class="selected">
</div> ---->

I am having trouble wrapping my head around toggleClass for some reason.

Currently, if i click on any one of the spans, the 'selected' sticks and doesnt leave when i click on any another span.

What am I missing?

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-09-23 22:36:51

我通常通过在将类添加到当前元素(您刚刚选择的元素)之前从所有相关元素中删除该类来实现此目的。

$(document).ready(function() {
 $(".views-field-field-success-stories-image-data .field-content").click(function() {

   // Add this next line
   $("#bannerContainer img").removeClass("selected");

   var sharedata = $(this).text();
   $('#bannerContainer img[title = "'+sharedata+'" ]').addClass('selected');
 });
 $("#bannerContainer img").click(function() {
  if (
   $('#bannerContainer img').hasClass('selected', $('#bannerContainer     img').removeClass('selected') ) );
 });
});

I usually accomplish this by just removing the class from all the related elements right before I add the class to the current element (the one you just selected).

$(document).ready(function() {
 $(".views-field-field-success-stories-image-data .field-content").click(function() {

   // Add this next line
   $("#bannerContainer img").removeClass("selected");

   var sharedata = $(this).text();
   $('#bannerContainer img[title = "'+sharedata+'" ]').addClass('selected');
 });
 $("#bannerContainer img").click(function() {
  if (
   $('#bannerContainer img').hasClass('selected', $('#bannerContainer     img').removeClass('selected') ) );
 });
});
子栖 2024-09-23 22:36:51

在一个元素上再次设置它之前,从所有拥有它的元素中删除“选定”类怎么样?

how about removing the 'selected' class from all elements that have it, before setting it again on the one element?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文