Jquery Toggle 如果 .text() 等于 img[title] 则更改图像
在上周社区的帮助下,我成功实现了这个 代码在这里
现在我需要做类似的事情,但现在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常通过在将类添加到当前元素(您刚刚选择的元素)之前从所有相关元素中删除该类来实现此目的。
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).
在一个元素上再次设置它之前,从所有拥有它的元素中删除“选定”类怎么样?
how about removing the 'selected' class from all elements that have it, before setting it again on the one element?